【问题标题】:Unpermitted parameter nested form inside a nested form嵌套表单中不允许的参数嵌套表单
【发布时间】:2018-11-30 12:26:19
【问题描述】:

我有一个 Workgroup 对象,它有很多 resource_quantities。 Resource_quantity 属于资源。我想要一个可以在一个地方创建工作组及其子资源数量的表单。我正在尝试使用嵌套属性构建表单。我的问题是,当我提交可以添加许多 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

我的 Workgroup 表单,其中集成了 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>

希望有人能帮助我,我是 Rails 新手

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    似乎通过将 resources_attributes 放入字符串中,我可以在控制台中获得正确的参数。

      simple_fields_for "resources_attributes" do |e| 
    

    我不知道这是否是一种好习惯,但效果很好。

    【讨论】:

      【解决方案2】:

      您可以尝试以下更改吗?

      替换

      <%= f.simple_fields_for :resources do |e| %>
      

      <%= f.simple_fields_for :resources_attributes do |e| %>
      

      【讨论】:

      • 好的,我已经尝试过您的解决方案,但没有帮助 :D 不过,当我将 simple_fields_for "resources_attributes" 放入 |e| 时它完成了这项工作。感谢您分享您的想法,无论如何它帮助我找到了解决方案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2022-07-05
      • 1970-01-01
      相关资源
      最近更新 更多