71

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.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Lilz
  • 4,013
  • 13
  • 61
  • 95

4 Answers4

141

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

starball
  • 20,030
  • 7
  • 43
  • 238
juanm55
  • 1,679
  • 1
  • 12
  • 9
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") %>

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Jonathan Leung
  • 2,051
  • 2
  • 19
  • 24
7

Maybe try this:

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

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

MattMcKnight
  • 8,185
  • 28
  • 35
2

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

bensie
  • 5,373
  • 1
  • 31
  • 34