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

查看:22
本文介绍了具有多个嵌套模型的 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.

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

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?

解决方案

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.

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

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