带有多个嵌套模型的 Rails 表单会导致无线电组出现问题 [英] Rails form with multiple nested models causes issues with radio groups

查看:51
本文介绍了带有多个嵌套模型的 Rails 表单会导致无线电组出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的模型包含一个 has_many 关系,如下所示:

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

然后我有一个使用

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

在这个表单中包含一组在 for 循环中创建的单选按钮

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}"

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

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.

当只有一个嵌套模型时,这里是 params[:order] 散列的内容:

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"=>"sdfg@sgf.com"}

当存在两个嵌套模型时:

And when two nested models are present:

{"order_items_attributes"=>
  [{"size"=>"Small",
    "colour_id"=>"4"},
   {"size"=>"Small"}],
 "first_name"=>"sdf",
 "last_name"=>"sdf",
 "email"=>"sdfg@sgf.com"}

正如你所看到的,只有第一个 order_item 有它的 colour_id 属性.无论所选单选按钮属于哪个模型,都会发生这种情况(这是有道理的).

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).

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

推荐答案

在调用 fields_for 时,您必须为每个订单项指定唯一索引.如果您以这种方式调用 fields_for,则需要跟踪传递给 fields_for 的数组的索引.Rails 可以通过使用嵌套表单为您做到这一点.

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%>

看起来您要发布到 orders_controller,所以这应该是替代品.

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

这篇关于带有多个嵌套模型的 Rails 表单会导致无线电组出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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