Rails:has_many:通过_ids嵌套?
在单个页面上,我可能有一个视频,然后通过复选框向其中添加多只狗。足够简单(如下所示)...
视图:
<%= check_box_tag "video[dog_ids][]", dog.id %>
控制器:
params[:video][:dog_ids] ||= []
但我不知道如何做是拥有多个视频,每个视频都有多只狗。
我目前有这个:
<% @videos.each do |video| %>
<%= fields_for "item[]", video do |f| %>
<%= f.hidden_field :id, :index => nil %>
<%= f.text_field :title, :index => nil%>
<%= f.text_area :body, :index => nil %>
<% video.dogs.each do |dog| %>
<%= check_box_tag "item[][video[dog_ids][]]", dog.id %>
<% end %>
<% end %>
<% end %>
但是当我这样做时,提交时 dogs_ids
总是 nil
。
知道我做错了什么吗?
On a single page I might have a single Video and then checkboxes to add multiple dogs to it. Easy enough (as follows)...
View:
<%= check_box_tag "video[dog_ids][]", dog.id %>
Controller:
params[:video][:dog_ids] ||= []
But what I can't figure out how to do is have multiple videos, each with multiple dogs.
I currently have this:
<% @videos.each do |video| %>
<%= fields_for "item[]", video do |f| %>
<%= f.hidden_field :id, :index => nil %>
<%= f.text_field :title, :index => nil%>
<%= f.text_area :body, :index => nil %>
<% video.dogs.each do |dog| %>
<%= check_box_tag "item[][video[dog_ids][]]", dog.id %>
<% end %>
<% end %>
<% end %>
But when I do that, dogs_ids
is always nil
when it's submitted.
Any idea what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这样的设置包含
fields_for "item[]"
和f.text_field :title, :index => nil
产生:这表明复选框名称应为
item[][dog_ids][]
。一个工作示例:
这会在参数中产生结果:
Such a setup with
fields_for "item[]"
andf.text_field :title, :index => nil
produces:This indicates that the checkbox name should be
item[][dog_ids][]
.A working example:
This produces result in params: