我有一个工作组对象,该对象具有许多resource_quantities。 Resource_quantity属于资源。我想要一种可以在一个地方创建工作组及其子级resource_quantities的表单。我正在尝试使用嵌套属性构建表单。我的问题是,当我提交表单时可以在其中添加许多resources_quantities及其相关资源时,我得到
Parameters: {"utf8"=>"✓", "workgroup"=>{"name"=>"Living room", "description"=>"installation floor", "contractor_id"=>"1", "resource_quantities_attributes"=>{"1543577850668"=>{"quantity"=>"22", "resources"=>{"name"=>"wood", "unit_type"=>"Matériel", "purchase_price"=>"20", "price"=>"22", "unit_measure"=>"u", "vat"=>"12"}, "_destroy"=>"false"}}}, "commit"=>"Create Workgroup"}
我收到资源参数而不是resource_attributes的事实导致了Unpermitted参数::resources。我已经尝试过此params.require(:workgroup).permit!但我仍然收到资源,导致ResourceQuantity的属性“资源”未知。
以下是有用的代码段:
工作组控制器
def new
@workgroup = Workgroup.new
@workgroup.resource_quantities.build.build_resource
end
def create
@workgroup = Workgroup.new(workgroup_params)
if @workgroup.save!
raise
redirect_to resources_path, notice: 'Resource was successfully created.'
else
render :new
end
end
def workgroup_params
params.require(:workgroup).permit(:name, :description, :contractor_id, :_destroy, {resource_quantities_attributes: [:quantity, :_destroy, {resources_attributes: [ :name, :unit_type, :price, :contractor_id, :purchase_price, :unit_measure, :vat]}]})
end
我的三个模型
class Workgroup < ApplicationRecord
has_many :resource_quantities, inverse_of: :workgroup
has_many :resources, through: :resource_quantities
accepts_nested_attributes_for :resource_quantities, allow_destroy: true
end
class ResourceQuantity < ApplicationRecord
belongs_to :workgroup, optional: true
belongs_to :resource, optional: true
accepts_nested_attributes_for :resource, :allow_destroy => true
end
class Resource < ApplicationRecord
has_many :workgroups, through: :resource_quantities
has_many :resource_quantities, inverse_of: :resource
end
我的工作组表单,其中集成了resource_quantities表单
<%= simple_form_for(@workgroup) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :description %>
<%= f.association :contractor, as: :hidden, input_html: {value: f.object.contractor || "#{current_user.contractor.id}"} %>
<h3> Resources </h3>
<table class='large_table'>
<tbody class="add_resource">
<%= f.simple_fields_for :resource_quantities do |builder| %>
<%= render 'resource_quantity_fields', f: builder %>
<% end %>
</tbody>
</table>
</div>
<div class="form-actions">
<%= f.button :submit %>
<%= link_to_add_association 'Ajouter une resource', f, :resource_quantities, class: 'btn btn-primary', data: { association_insertion_node: '.add_resource', association_insertion_method: :append } %>
</div>
<% end %>
这是我的部分,用于添加与resource_quantity和resource相关的字段
<tr class="nested-fields">
<td>
<%= f.input :quantity %>
</td>
<%= f.simple_fields_for :resources do |e| %>
<td><%= e.input :name %></td>
<td><%= e.input :unit_type, collection: Resource::TYPES %></td>
<td><%= e.input :purchase_price %></td>
<td><%= e.input :price %></td>
<td><%= e.input :unit_measure, collection: Resource::UNIT_MEASURE%></td>
<td><%= e.input :vat, collection: Resource::VAT%></td>
<td>
<%= link_to_remove_association theme_icon_tag("trash"), f %>
</td>
<% end %>
</tr>
希望有人能够帮助我,我是新秀。
答案 0 :(得分:1)
似乎通过将resources_attributes放入字符串中,使我能够在控制台中获得正确的参数。
simple_fields_for "resources_attributes" do |e|
我不知道这是否是个好习惯,但是效果很好。
答案 1 :(得分:0)
您可以尝试以下更改吗?
替换
<%= f.simple_fields_for :resources do |e| %>
使用
<%= f.simple_fields_for :resources_attributes do |e| %>