Rails form with multiple nested models causes issues with radio groups

試著忘記壹切 提交于 2019-11-29 07:23:28

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!