Friday 15 June 2012

Rails 3, add a link_to to add a product id to a session variable -



Rails 3, add a link_to to add a product id to a session variable -

i have simple app single model of product name , cost attributes. in add-on standard scaffolding actions in index view add together link_to action 'add_to_cart' add together products id session variable. far have...

index.html.erb

<% @products.each |product| %> <tr> <td><%= product.name %></td> <td><%= product.price %></td> <td><%= link_to 'show', product %></td> <td><%= link_to 'edit', edit_product_path(product) %></td> <td><%= link_to 'destroy', product, method: :delete, data: { confirm: 'are sure?' } %></td> <td><%= link_to "add cart", :controller => 'product', :action => 'add_to_cart' %></td> </tr> <% end %> </table>

controller

def add_to_cart #add session here end

i'm not sure route should add together create work. have feeling link_to line incorrect.

any help appreciated.

in routes (assuming using resource route products) (note put, since changing state):

# routes.rb resources :products fellow member set :add_to_cart end end

then, link in view be:

<%= link_to "add cart", add_to_cart_product_path(product), :method => :put %>

also, prepare spelling of controller action:

def add_to_cart ... end

ruby-on-rails

No comments:

Post a Comment