Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass parameter by link_to ruby on rails

I have this line of code:

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

when im in the add_to_cart method...how can i call the :car please?

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

That doesn't work because it says that I'm trying to stringify it.

I don't understand what's wrong; because I used this to create new users and it worked fine.

By the way, car is my car object.

like image 782
Lilz Avatar asked Dec 14 '09 03:12

Lilz


2 Answers

Try:

<%= 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 find in your 'cars' table (as with rails pluralization) in your DB a car with id == to car.id

hope it helps! happy coding

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

like image 115
juanm55 Avatar answered Sep 20 '22 13:09

juanm55


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") %>

like image 45
Jonathan Leung Avatar answered Sep 17 '22 13:09

Jonathan Leung