Я пытаюсь создать форму с вложенными ресурсами в моем приложении 4 рельсов. Я использую драгоценный камень кокона. Каждый шаг будет иметь подшаги, и я бы хотел, чтобы пользователь добавил столько подэтапов в форму и хотел бы.Rails cocoon вложенная форма получает только один первый вложенный атрибут
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"}
Любые мысли по моей проблеме?
EDIT Совместное использование 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">
Я думаю, вы исправили проблему упоминаемый @BroiSatse , Можете ли вы добавить html, который создается после добавления нескольких подстановок? –
@MichalSzyndel добавил HTML-код – SlimReaper
Интересно, он должен работать по-моему –