rails嵌套表单不提交父类

时间:2017-12-12 15:14:46

标签: ruby-on-rails

以下观点

   <%= form_for(@event) do |f| %>
    <%# @event.eventdistances.in_groups_of(6, false) do |eventdistances| %>
    <div class='row'>
      <% @event.eventdistances.each do |ed| %>
        <div class='small-2 medium-2 large-2 columns'>
          <%= fields_for "eventdistances[]", ed do |ff| %>
            <%= ff.check_box :active %> <%= ed.struttura.nome %>
          <% end %>
        </div>
      <% end %>
    </div>
    <%# end %>

生成一个请求,该请求具有父对象的id,但不会在params哈希中调用它。

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"l5xHBsUuL5uM/ip2P0jWLxMi46zhVOxo1vyei9xdLPDFxL3IHmVBAhz/vfu1esJjIBZbElG2pefp+zbAC1vivQ==", "eventdistances"=>{"1"=>{"active"=>"0"}}, "commit"=>"Confirm changes", "id"=>"5"}

当然,强大的params抱怨因为

中缺少了参数
params.require(:event).permit(*permitted)

缺少这种形式?

1 个答案:

答案 0 :(得分:0)

  <%= f.fields_for :eventdistances do |ff| %>

是正确的语法,同时确保强大的参数包括id

eventdistances_attributes: [:id,  ... ]