通过link_to在ruby上传递参数 [英] pass parameter by link_to ruby on rails

查看:79
本文介绍了通过link_to在ruby上传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这行代码:

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

当我在add_to_cart方法中...我该如何呼叫: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.

推荐答案

尝试:

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

然后在您的控制器中

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

在数据库中的汽车"表中(与铁路复数形式一样)会找到ID ==到car.id的汽车

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

一年多以后,但是如果您看到它或任何人看到,我都可以使用; D

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

这篇关于通过link_to在ruby上传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆