ruby-on-rails - 通过 link_to ruby​​ on Rails 传递参数

标签 ruby-on-rails parameters

我有这行代码:

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

当我使用 add_to_cart 方法时...我该如何调用 :car?

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

这不起作用,因为它说我正在尝试将其字符串化。

我不明白出了什么问题;因为我用它来创建新用户并且效果很好。

顺便说一句,汽车是我的汽车对象。

最佳答案

尝试:

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

然后在你的 Controller 中

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

它将在数据库中的“汽车”表(与 Rails 复数形式相同)中找到 id == car.id 的汽车

关于ruby-on-rails - 通过 link_to ruby​​ on Rails 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1898737/

相关文章:

hibernate - Hibernate @Formula中TRIM函数的参数数量错误

parameters - 将 SELECT-OPTIONS 表作为参数传递给子例程?

ruby-on-rails - rails : what is the best way to check if the record doesn't exist in the database?

ruby-on-rails - 使用哈希键获取哈希索引

ruby-on-rails - RSpec 错误 "undefined method ` respond_with'..."

c# - 从 C# 将参数传递给 powershell

sql - "x IS NULL"和 "NOT (x IS NOT NULL)"有什么区别?

ruby-on-rails - Ruby中类定义中的方法类是什么意思?

python - 强制某些设置需要可选参数

linux - git别名不接收参数