pass parameter by link_to ruby on rails

后端 未结 4 788
孤城傲影
孤城傲影 2020-11-30 21:58

I have this line of code:

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

wh

4条回答
  •  旧巷少年郎
    2020-11-30 22:23

    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

提交回复
热议问题