具有多个嵌套模型的 Rails 形式会导致无线电组出现问题

发布于 2024-08-07 12:08:51 字数 1801 浏览 9 评论 0原文

我遇到了包含单选按钮的嵌套模型表单的问题,当我有多个模型时,所有单选按钮都被视为位于同一组中。

我的模型包含这样的 has_many 关系:

class Order < ActiveRecord::Base
    has_many :order_items
    accepts_nested_attributes_for :order_items
end

Class OrderItem < ActiveRecord::Base
    belongs_to :order
end

然后我有一个部分,它使用以下命令创建 OrderItem 模型表单

<% fields_for "order[order_items_attributes][]", order_item do |f| %>

并且包含在该表单中的是在 for 循环内创建的一组单选按钮

radio_button_tag "order[order_items_attributes][][colour_id]", "#{colour.id}"

这在存在时工作正常只有一个子项,但是一旦我插入多个子项,所有单选按钮都属于同一组,因为它们都具有相同的属性 name="order[order_items_attributes][][colour_id]"。这一切都在一个新的模型表单上,所以我不能使用数组索引(name="order[order_items_attributes][0][colour_id]"),因为Rails给出了错误expected Hash ( get Array) for param 'order_items_attributes' 我对最后一部分的理解是错误的,错误是因为我混合了索引和非索引名称属性。添加索引值是解决此问题的关键。

以下是仅存在一个嵌套模型时 params[:order] 哈希的内容:

{"order_items_attributes"=>
  [{"size"=>"Small",
    "colour_id"=>"4"],
 "first_name"=>"sdf",
 "last_name"=>"sdf",
 "email"=>"[email protected]"}

存在两个嵌套模型时:

{"order_items_attributes"=>
  [{"size"=>"Small",
    "colour_id"=>"4"},
   {"size"=>"Small"}],
 "first_name"=>"sdf",
 "last_name"=>"sdf",
 "email"=>"[email protected]"}

以及 您只能看到第一个 order_item 具有它的 color_id 属性。无论所选单选按钮属于哪个模型,都会发生这种情况(这是有道理的)。

如何渲染单选按钮,以便为每个子模型创建单独的组?

I'm having a problem with nested model forms that contain radio buttons, when I have multiple models all the radio buttons are treated as being in the same group.

My model contains a has_many relationship like this:

class Order < ActiveRecord::Base
    has_many :order_items
    accepts_nested_attributes_for :order_items
end

Class OrderItem < ActiveRecord::Base
    belongs_to :order
end

I then have a partial that creates the OrderItem model form using

<% fields_for "order[order_items_attributes][]", order_item do |f| %>

And contained within this form is a group of radio buttons created inside a for loop with

radio_button_tag "order[order_items_attributes][][colour_id]", "#{colour.id}"

This works fine when there is only one child, however as soon as I insert multiple children all the radio buttons belong to the same group as they all have the same attribute name="order[order_items_attributes][][colour_id]". This is all on a new model form so I can't use array indexes (name="order[order_items_attributes][0][colour_id]") as Rails gives the error expected Hash (got Array) for param 'order_items_attributes' I was wrong about that last part, error was because I was mixing indexed and non-indexed name attributes. Adding index values was the key to solving this.

Here is the contents of the params[:order] hash when only one nested model is present:

{"order_items_attributes"=>
  [{"size"=>"Small",
    "colour_id"=>"4"],
 "first_name"=>"sdf",
 "last_name"=>"sdf",
 "email"=>"[email protected]"}

And when two nested models are present:

{"order_items_attributes"=>
  [{"size"=>"Small",
    "colour_id"=>"4"},
   {"size"=>"Small"}],
 "first_name"=>"sdf",
 "last_name"=>"sdf",
 "email"=>"[email protected]"}

As you can see only the first order_item has it's colour_id attribute. This occurs regardless of which model the selected radio button belonged to (which makes sense).

How can I render the radio buttons such that it creates a separate group for each child model?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏末 2024-08-14 12:08:51

当您调用 fields_for 时,您必须为每个订单项指定一个唯一索引。如果您以这种方式调用 fields_for,则需要跟踪传递给 fields_for 的数组的索引。 Rails 可以通过使用嵌套表单来为您完成此操作。

解决方案是使用嵌套表单。

<%form_for :order do |f|%>
  Form stuff for this particular order.
  If @order.order_items is empty you may need to build one before the next line.
  <%f.fields_for :order_items do |oi_f| %>
    Form stuff for this particular order_item (prefixed with oi_f.)
    <%Colour.all.each do |colour| %>
      <%=oi_f.radio_tag(:colour_id, colour.id)%>
    <%end%>
  <%end%>
<%end%>

看起来您正在发布到orders_controller,因此这应该是替代品。

You must give each order item a unique index when you call fields_for. If you're calling fields_for in this manner, you need to keep track of the index of the array you pass to fields_for. Rails can do this for you by using nested forms.

The solution is to use nested forms.

<%form_for :order do |f|%>
  Form stuff for this particular order.
  If @order.order_items is empty you may need to build one before the next line.
  <%f.fields_for :order_items do |oi_f| %>
    Form stuff for this particular order_item (prefixed with oi_f.)
    <%Colour.all.each do |colour| %>
      <%=oi_f.radio_tag(:colour_id, colour.id)%>
    <%end%>
  <%end%>
<%end%>

Looks like you're posting to the orders_controller so this should be a drop in replacement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文