在基本 RoR 应用程序中保存 has_and_belongs_to_many 链接
我尝试了解我的两个全新且简单的模型产品和作者之间的 has_and_belongs_to_many 关系,其中产品可以有许多作者,而作者可以有很多产品。
我这样写:
class Author < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :authors
end
在产品视图的部分形式中,我有:
<p>Products</p>
<%= collection_select(:product, :author_ids, @authors, :id, :name, :prompt => " ", :multiple => true) %>
但是当我点击更新按钮时,我收到这个奇怪的消息,我自己无法解决:
NoMethodError in ProductsController#update “1”的未定义方法“拒绝”:字符串
Rails.root:/home/stephane/www/HABTM 应用追踪 |框架跟踪 |完整追踪 app/controllers/products_controller.rb:63:in 阻止更新' app/controllers/products_controller.rb:62:in
更新' 请求
参数: {"utf8"=>"âœ"", "_method"=>"放置", “authenticity_token”=>“2GlTssOFjTVZ9BikrIFgx22cdTOIJuAB70liYhhLf+4=”, "product"=>{"title"=>"Le trésor des Templiers", "original_title"=>"", “数字”=>“1”, "added_by"=>"", “author_ids”=>“1”}, "提交"=>"更新产品", “id”=>“1”}
出了什么问题? :product_ids 有问题吗...我在互联网上看到我必须输入“s”,但我不确定它代表什么...
我如何将表authors_products 链接到返回的密钥通过下拉菜单? (此处“author_ids”=>“1”) 谢谢 !
更多信息: 可以使用此信息解决,但仍然无法保存关系:
collection_select("sales_agent", "customer_id", @customers, "id", "name")
假设您有一个具有 ID 属性和名称属性的客户模型,这将产生完全相同的上述代码。因此,查看我们传递到 collection_select 调用中的值:
- 第一个参数是包含集合中一个元素的模型(例如 sales_agent)
- 接下来是模型中引用其包含的集合元素的字段名称(例如 customer_id )
- 接下来是包含我们要列出的项目集合的变量(例如@customers)
- 接下来是选项标签的 value 属性(例如客户 ID)
- 接下来是选项标签的显示属性(例如客户名称)
所以我现在写
<p>Products</p>
<%= collection_select(:author, :author_id, @authors, :id, :name, :prompt => " ", :multiple => true) %>
了有效,但暂时没有保存链接...(仅保存正常字段的更新,而不保存关系:-(
I try to learn the has_and_belongs_to_many relationship between my 2 fresh new and simple models Product and Author, where a Product can have many authors and where author can have a lots of products.
I wrote this :
class Author < ActiveRecord::Base
has_and_belongs_to_many :products
end
class Product < ActiveRecord::Base
has_and_belongs_to_many :authors
end
In the partial form of view for the products, I have :
<p>Products</p>
<%= collection_select(:product, :author_ids, @authors, :id, :name, :prompt => " ", :multiple => true) %>
but when I hit the update button, I get this strange message I can't resolve myself :
NoMethodError in ProductsController#update
undefined method `reject' for "1":String
Rails.root: /home/stephane/www/HABTM
Application Trace | Framework Trace | Full Trace
app/controllers/products_controller.rb:63:in block in update'
update'
app/controllers/products_controller.rb:62:in
Request
Parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"2GlTssOFjTVZ9BikrIFgx22cdTOIJuAB70liYhhLf+4=",
"product"=>{"title"=>"Le trésor des Templiers",
"original_title"=>"",
"number"=>"1",
"added_by"=>"",
"author_ids"=>"1"},
"commit"=>"Update Product",
"id"=>"1"}
What's wrong ? Is there a problem with :product_ids... I saw on internet I had to pu a "s" but I'm not sure of what it represents....
How can I link the table authors_products to the key which is given back by the drop-down menu ? (here "author_ids"=>"1")
Thx !
More info :
May be solved with this info, but still no saving of the relationship :
collection_select("sales_agent", "customer_id", @customers, "id", "name")
Assuming you had a customer model with an ID attribute and a name attribute, this would produce exactly the above code. So looking at the values we pass into the collection_select call:
- The first parameter is the model that contains one element from the collection (eg. sales_agent)
- Next is the field name in the model that refers to the collection element it contains (eg. customer_id)
- Next is the variable containing the collection of items that we want to list (eg. @customers)
- Next is the value attribute of the option tag (eg. the customer id)
- Next is the display attribute of the option tag (eg. the customer name)
So I now wrote
<p>Products</p>
<%= collection_select(:author, :author_id, @authors, :id, :name, :prompt => " ", :multiple => true) %>
and it worked, but without saving the link, for the moment... (only the update of the normal fields are saved, not the relationship :-(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否有一个名为
author_products
的用于 HABTM 关系的单独模型?您需要通过执行诸如
rails g modelauthor_product
之类的操作来运行另一次迁移,并且该表应仅包含两个字段:确保没有主键。
像这样的东西:
Do you have a separate model called
author_products
for the HABTM relationship?You'll need to run another migration by doing something like
rails g model author_product
and the table should only contain two fields:Make sure there is no primary key.
Something like: