本文介绍了Rails 茧嵌套表单只接收一个第一个嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 rails 4 应用程序中构建一个包含嵌套资源的表单.我正在使用茧宝石.每个步骤都有子步骤,我希望允许用户在表单中添加任意数量的子步骤.

步骤.rb

class Step 

子步骤.rb

class Substep <ActiveRecord::Base归属地:步骤

表单代码

step_path 做 |f|%><%= text_field(:step, :title, :value => '', class: 'fly-input input_info', placeholder: 'Process Title', id: 'step_form_title') %><%= text_field(:step, :description, :value => '', class: 'fly-input input_info', placeholder: 'Process Description', id: 'step_form_description') %><%= hidden_​​field :step, :known %><%= hidden_​​field_tag :experiment, @experiment.id %><%= f.fields_for :substep do |ff|%><%= 渲染 "substep_fields", :f =>ff%><%结束%><%= link_to_add_association '添加子步骤', f, :substeps %><%= f.submit "Done", class: "main_button" %><%结束%>

_substep_fields.html.erb

<%= f.text_field :description %>

steps_controller.rb

def step_paramsparams.require(:step).permit(:title, :description, :known, substeps_attributes: [:id, :description, :action, :_destroy])结尾

我可以看到第一个输入,并为多个子步骤添加第二、第三等输入.问题是,只有第一个子步骤的描述会传递给控制器​​:

(byebug) 参数参数{utf8"=>✓",authenticity_token"=>xxx",step"=>{title"=>测试标题",描述"=>测试描述",已知"=>-1",子步骤"=>{描述"=>子步骤1"}},实验"=>64",提交"=>完成",控制器"=>步骤",动作"=>创建"}

对我的问题有什么想法吗?

编辑添加两个子步骤后生成的共享HTML

<input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="xxxx"><input value="" class="fly-input input_info ui-autocomplete-input" placeholder="Process Title" id="step_form_title" type="text" name="step[title]" data-hasqtip="6"aria-描述的=qtip-6"自动完成=关闭"><label class="fly-label classic is-active" for="classic">描述</label><input value="" class="fly-input input_info" placeholder="Process Description" id="step_form_description" type="text" name="step[description]" data-hasqtip="7" aria-descriptionby=qtip-7"><input type="hidden" name="step[known]" id="step_known" value="-1"><input type="hidden" name="experiment" id="experiment" value="76"></表单><div class="nested-fields"><input type="text" name="step[substeps_attributes][1453146839656][description]" id="step_substeps_attributes_1453146839656_description">

<div class="nested-fields"><input type="text" name="step[substeps_attributes][1453146844032][description]" id="step_substeps_attributes_1453146844032_description">

<div class="" id="customprocess_processsteps"><a class="main_button add_fields" data-association="substep" data-associations="substeps" data-association-insertion-template="<div class='nested-fields'><输入类型=&quot;文本&quot;name="step[substeps_attributes][new_substeps][description]"id="step_substeps_attributes_new_substeps_description"/></div>" href="#">添加子步骤</a><button aria-label="关闭显示" class="close-button" data-close="" type="button">

<input type="submit" name="commit" value="Done" class="main_button">

解决方案

您需要与 fields_for 中的关联名称完全匹配.改变这个:

Cocoon 需要一个严格的 DOM 结构,尝试改变

致:

<%= f.fields_for :substep do |ff|%><%= 渲染 "substep_fields", :f =>ff%><%结束%><div class="links"><%= link_to_add_association '添加子步骤', f, :substeps %>

如果我没记错的话,只需要 link_to_add_association 的包装器,最顶级的包装器就很好.

I am trying to build a form with nested resources in my rails 4 app. I am using the cocoon gem. Each step will have substeps, and I'd like to allow the user to add as many substeps to the form and he/she would like.

Step.rb

class Step < ActiveRecord::Base
  has_many :substeps
  accepts_nested_attributes_for :substeps

Substep.rb

class Substep < ActiveRecord::Base
  belongs_to :step

form code

<%= form_for Step.new, :url => steps_path do |f| %>
  <%= text_field(:step, :title, :value => '', class: 'fly-input input_info', placeholder: 'Process Title', id: 'step_form_title') %>
  <%= text_field(:step, :description, :value => '', class: 'fly-input input_info', placeholder: 'Process Description', id: 'step_form_description') %>
  <%= hidden_field :step, :known %>
  <%= hidden_field_tag :experiment, @experiment.id %>
  <%= f.fields_for :substep do |ff| %>
    <%= render "substep_fields", :f => ff %>
  <% end %>
  <%= link_to_add_association 'Add substep', f, :substeps %>
  <%= f.submit "Done", class: "main_button" %>
<% end %>

_substep_fields.html.erb

<div class='nested-fields'>
  <%= f.text_field :description %>
</div>

steps_controller.rb

def step_params
  params.require(:step).permit(:title, :description, :known, substeps_attributes: [:id, :description, :action, :_destroy])
end

I can see the first input, and add second, third, so on inputs for multiple substeps. The problem is, only the first substep's description will be passed to the controller:

(byebug) params
params
{"utf8"=>"✓", "authenticity_token"=>"xxx", "step"=>{"title"=>"Test Title", "description"=>"Test Desc", "known"=>"-1", "substep"=>{"description"=>"Substep1"}}, "experiment"=>"64", "commit"=>"Done", "controller"=>"steps", "action"=>"create"}

Any thoughts on my problem?

EDITSharing HTML generated after adding two substeps

<form class="new_step" id="new_step" action="/steps" accept-charset="UTF-8" method="post">
<input name="utf8" type="hidden" value="✓"><input type="hidden" name="authenticity_token" value="xxxx">
  <input value="" class="fly-input input_info ui-autocomplete-input" placeholder="Process Title" id="step_form_title" type="text" name="step[title]" data-hasqtip="6" aria-describedby="qtip-6" autocomplete="off">
  <label class="fly-label classic is-active" for="classic">Description</label>
  <input value="" class="fly-input input_info" placeholder="Process Description" id="step_form_description" type="text" name="step[description]" data-hasqtip="7" aria-describedby="qtip-7">
  <input type="hidden" name="step[known]" id="step_known" value="-1">
  <input type="hidden" name="experiment" id="experiment" value="76">
</form>
  <div class="nested-fields">
    <input type="text" name="step[substeps_attributes][1453146839656][description]" id="step_substeps_attributes_1453146839656_description">
  </div>
  <div class="nested-fields">
    <input type="text" name="step[substeps_attributes][1453146844032][description]" id="step_substeps_attributes_1453146844032_description">
  </div>
  <div class="" id="customprocess_processsteps">
    <a class="main_button add_fields" data-association="substep" data-associations="substeps" data-association-insertion-template="<div class='nested-fields'>
  <input type=&quot;text&quot; name=&quot;step[substeps_attributes][new_substeps][description]&quot; id=&quot;step_substeps_attributes_new_substeps_description&quot; />
</div>" href="#">Add substeps</a>
    <button aria-label="Close reveal" class="close-button" data-close="" type="button">
    </button>
  </div>
<input type="submit" name="commit" value="Done" class="main_button">
解决方案

You need to match exactly the name of the association within fields_for. Change this:

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

To

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

EDIT: Cocoon expects a strict DOM structure to work, try changing

<%= f.fields_for :substep do |ff| %>
  <%= render "substep_fields", :f => ff %>
<% end %>
<%= link_to_add_association 'Add substep', f, :substeps %>

To:

<div id="substeps">
  <%= f.fields_for :substep do |ff| %>
    <%= render "substep_fields", :f => ff %>
  <% end %>
  <div class="links">
    <%= link_to_add_association 'Add substep', f, :substeps %>
  </div>
</div>

If I remember correctly, only wrapper around link_to_add_association is required, the most top-level wrapper is just nice to have.

这篇关于Rails 茧嵌套表单只接收一个第一个嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 19:22