我有一种复杂的形式来构建。我正在使用茧和简单的形式来构建它。 在该应用程序中有合同,对于每个合同,用户应该能够通过该表单创建特定类型的多个分发。
我的模型结构如下所示(只是你可以想象):
class Contract < ApplicationRecord
has_many distributions
accepts_nested_attributes_for :distributions
end
class Distribution
belongs_to :contract
# distribution_type :string // e.g. month or city
# percentage
# value
end
我想要实现的是让用户通过位于“_distribution_fields.html.erb”部分内的下拉列表选择distribution_type。如果用户选择某种类型,例如“月”,则通过javascript加载预定义的“div”,并按如下方式一次构建多个字段:
<div class="nested-fields">
<div class="row">
<div class="row">
<%= f.input :distribution_type, wrapper_html: {class: 'col-xs-6 col-md-2'}, input_html: {data: {'toggle-select': 'distribution-type-0'}, class: 'distribution_type_select'}, required: true, disabled: @readonly %>
</div>
<div class="row">
<div id="distribution-type-0-event_type" class="distribution-type-selection">
<%= hidden_field_tag 'event_type', false, class: 'distribution_type_hidden_event_type' %>
<% @contract.event_types.each do |event_type| %>
<div class="col-xs-6 col-md-4">
<div class="field">
<%= f.input :distribution_type, as: 'hidden', input_html: { class: 'event_type_distribution_hidden', value: 'event_type' } %>
<%= f.input :value, input_html: { readonly: true, value: event_type } %>
<%= f.input :percentage, required: true, disabled: @readonly %>
</div>
</div>
<% end %>
</div>
</div>
<div class="row">
<div id="distribution-type-0-month" class="distribution-type-selection">
<%= hidden_field_tag 'month', false, class: 'distribution_type_hidden_month' %>
<% @contract.month_date_list.each do |month| %>
<div class="col-xs-6 col-md-4">
<div class="field">
<%= f.input :distribution_type, as: 'hidden', input_html: { class: 'month_distribution_hidden', value: 'month' } %>
<%= f.input :value, input_html: { readonly: true, value: month } %>
<%= f.input :percentage, required: true, disabled: @readonly %>
</div>
</div>
<% end %>
</div>
</div>
<div class="row">
<div id="distribution-type-0-city" class="distribution-type-selection">
<%= hidden_field_tag 'city', false, class: 'distribution_type_hidden_city' %>
<% @contract.location_cities.each do |city| %>
<div class="col-xs-6 col-md-4">
<div class="field">
<%= f.input :distribution_type, as: 'hidden', input_html: { class: 'city_distribution_hidden', value: 'city' } %>
<%= f.input :value, input_html: { readonly: true, value: city } %>
<%= f.input :percentage, required: true, disabled: @readonly %>
</div>
</div>
<% end %>
</div>
</div>
<div class="col-xs-6 col-md-1">
<div class="row">
<div class="col-xs-12">
<%= link_to_remove_association f, class: 'btn btn-danger delete-btn', disabled: @readonly, data: { confirm: 'You are about to delete the internet as well as to remove this kpi distribution? Are you sure?' } do %>
<i class="fa fa-trash"></i>
<% end %>
</div>
</div>
</div>
在周围的“_form.html.erb”中,它如下:
...
<%= field_set_tag 'KPI Distribution' do %>
<%= f.simple_fields_for :distributions do |distribution| %>
<%= render 'distribution_fields', f: distribution %>
<% end %>
<div class="links m-b-md">
<div class="text-left">
<%= link_to_add_association f, :kpi_distributions, class: 'btn btn-info btn-sm', disabled: @readonly do %>
<i class="fa fa-plus"></i>
<% end %>
</div>
</div>
<% end %>
...
正如您在上面部分中已经认识到的那样,有一个“simple_fields_for”指令循环“_distribution_fields”部分。通常只在包含其所有输入的单个字段中创建这样的部分,但我想一次设置多个。做某事的某种方式有什么用吗?是否有任何最佳实践来实现这样的目标?或者我可以手动设置由cocoon产生的ID吗?
最好的问候