[ACCEPTED]-pass parameter by link_to ruby on rails-parameters

Accepted answer
Score: 141


<%= link_to "Add to cart", {:controller => "car", :action => "add_to_cart", :car => car.id }%>

and then in your controller

@car = Car.find(params[:car])

which, will 5 find in your 'cars' table (as with rails 4 pluralization) in your DB a car with id 3 == to car.id

hope it helps! happy coding

more 2 than a year later, but if you see it or 1 anyone does, i could use the points ;D

Score: 80

The above did not work for me but this did

<%= link_to "text_to_show_in_url", action_controller_path(:gender => "male", :param2=> "something_else") %>


Score: 6

Maybe try this:

<%= link_to "Add to cart", 
            :controller => "car", 
            :action => "add_to_cart", 
            :car => car.attributes %>

But I'd really like to see 2 where the car object is getting setup for 1 this page (i.e., the rest of the view).

Score: 2

You probably don't want to pass the car object 3 as a parameter, try just passing car.id. What 2 do you get when you inspect(params) after clicking "Add 1 to cart"?

More Related questions