"car", :action => "add_to_cart", :c...">

Pass by link_to ruby โ€‹โ€‹on rails parameter

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: car please?

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

This does not work, because he says that I am trying to strengthen it.

I do not understand what happened; because I used this to create new users and it worked fine.

By the way, the car is my car.

+59
ruby-on-rails parameters
Dec 14 '09 at 3:22
source share
4 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 โ€œcarsโ€ in your table (for example, with rail pluralization) in your database a car with id == to car.id

hope this helps! happy coding

more than a year later, but if you see it or anyone else, I can use the dots; D

+124
Aug 05 2018-11-11T00:
source share
โ€” -

Above did not work for me, but this one did

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

+68
Jul 26 '11 at 7:09
source share

Perhaps try the following:

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

But I really would like to see where the car object gets the setting for this page (i.e. the rest of the view).

+6
Dec 14 '09 at 4:06
source share

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

+2
Dec 14 '09 at 3:27
source share



All Articles