我正在尝试使用rails 4 app中的嵌套资源构建表单。我正在使用茧宝石。每个步骤都有子步骤,我想允许用户在表单中添加任意数量的子步骤,并且他/她想要。
Step.rb
class Step < ActiveRecord::Base
has_many :substeps
accepts_nested_attributes_for :substeps
Substep.rb
class Substep < ActiveRecord::Base
belongs_to :step
表单代码
<%= 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
我可以看到第一个输入,并添加第二个,第三个,等等多个子步骤的输入。问题是,只有第一个子步骤的描述将被传递给控制器:
(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"}
对我的问题有什么看法?
修改 共享添加两个子步骤后生成的HTML
<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="text" name="step[substeps_attributes][new_substeps][description]" id="step_substeps_attributes_new_substeps_description" />
</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">
答案 0 :(得分:2)
您需要在fields_for
内准确匹配关联的名称。改变这个:
<%= f.fields_for :substep do |ff| %>
要
<%= f.fields_for :substeps do |ff| %>
编辑:Cocoon希望严格的DOM结构能够工作,尝试改变
<%= f.fields_for :substep do |ff| %>
<%= render "substep_fields", :f => ff %>
<% end %>
<%= link_to_add_association 'Add substep', f, :substeps %>
要:
<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>
如果我没记错的话,只需要link_to_add_association
的包装,最顶级的包装就好了。
答案 1 :(得分:0)
您需要进行两项更改。一个是BroiSatse写的,改变了
var result = Array.ofDim[Array[Int]](Array.empty[Int])
到
<%= f.fields_for :substep do |ff| %>
第二,改变
<%= f.fields_for :substeps do |ff| %>
到
params.require(:step).permit(..., substeps_attributes: [:id, :description, :action, :_destroy])