繁体   English   中英

Rails 5.1强参数问题中的未知参数

[英]Unknown parameter in Rails 5.1 strong parameters issue

所以我的reconciliation模型如下所示:

class Reconciliation < ApplicationRecord
  belongs_to :location
  belongs_to :company
  has_and_belongs_to_many :inventory_items
  accepts_nested_attributes_for :inventory_items, allow_destroy: true
end

我的InventoryItem模型如下所示:

class InventoryItem < ApplicationRecord
  belongs_to :product
  belongs_to :location, inverse_of: :inventory_items
  has_and_belongs_to_many :reconciliations
end

在我的ReconciliationsController ,这就是我的reconciliation_params样子:

  def new
    @location = Location.find(params[:location_id])
    @reconciliation = @location.reconciliations.new
    @inventory_items = @location.inventory_items
    @start_index = 0
    @next_index = @start_index + 1
  end

def reconciliation_params
  params.require(:reconciliation).permit(:inventory_item_id, :location_id, :display_id, :inventory_items,
        inventory_items_attributes: [:id, :quantity_left, :quantity_delivered, :_destroy]
  )
end

这是我的routes.rb的相关部分:

  resources :locations, shallow: true do
    resources :inventory_items
    resources :reconciliations
  end

这是我的views/reconciliations/_form.html.erb

<%= simple_form_for @reconciliation, url: :location_reconciliations do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :location_id, as: :hidden %>
    <%= f.simple_fields_for :inventory_item do |inventory| %>
      <%= inventory.input :quantity_left %>
      <%= inventory.input :quantity_delivered %>
    <% end %>
  </div>

  <div class="form-actions">
      <%= f.button :submit, "Update", class: "btn btn-primary" %>
  </div>
<% end %>

这是我的app/views/reconciliations/new.html.erb

<% if params[:next].nil? %>
  <%= render 'form', reconciliation: @reconciliation, inventory_item: @inventory_items[@start_index] %>
<% else %>
  <%= render 'form', reconciliation: @reconciliation, inventory_item: @inventory_items[@next_index] %>
<% end %>

当我尝试创建reconciliation对象时,这是我的日志:

Started POST "/locations/2/reconciliations" for 127.0.0.1 at 2018-03-24 23:16:33 -0500
Processing by ReconciliationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"JZvhwloo0+XM9bmptxXGfnDw==", "reconciliation"=>{"location_id"=>"2", "inventory_item"=>{"quantity_left"=>"1", "quantity_delivered"=>"170"}}, "commit"=>"Update", "location_id"=>"2"}
Unpermitted parameter: :inventory_item
  Location Load (0.9ms)  SELECT  "locations".* FROM "locations" WHERE "locations"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
   (0.6ms)  BEGIN
   (0.7ms)  ROLLBACK
  Rendering reconciliations/new.html.erb within layouts/application
  InventoryItem Load (1.0ms)  SELECT "inventory_items".* FROM "inventory_items" WHERE "inventory_items"."location_id" = $1  [["location_id", 2]]
  Product Load (1.0ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  Rendered reconciliations/_form.html.erb (45.9ms)
  Rendered reconciliations/new.html.erb within layouts/application (66.8ms)
  Rendered shared/_navbar.html.erb (1.3ms)
Completed 200 OK in 202ms (Views: 115.1ms | ActiveRecord: 29.1ms)

我试过简单地添加:inventory_item到我的params.require(:reconciliation).permit(..) ,但这不起作用。

我错过了什么?

编辑1

当我在我的表单上检查HTML输入时,在simple_fields_for ,HTML看起来很好:

<input class="string required" type="text" name="reconciliation[inventory_item][quantity_left]" id="reconciliation_inventory_item_quantity_left">

编辑2

当我将simple_fields_for调用更改为复数时,即:inventory_items ,而不是:inventory_item如下所示:

表格的整个部分完全消失了。

这就是HTML的样子:

<div class="form-inputs">
    <div class="input hidden reconciliation_location_id"><input class="hidden" type="hidden" value="2" name="reconciliation[location_id]" id="reconciliation_location_id"></div>
</div>

这就是当simple_field_for :inventory_item是单数时HTML的外观:

<div class="form-inputs">
    <div class="input hidden reconciliation_location_id"><input class="hidden" type="hidden" value="2" name="reconciliation[location_id]" id="reconciliation_location_id"></div>

      <div class="input string required reconciliation_inventory_item_quantity_left"><label class="string required" for="reconciliation_inventory_item_quantity_left"><abbr title="required">*</abbr> Quantity left</label><input class="string required" type="text" name="reconciliation[inventory_item][quantity_left]" id="reconciliation_inventory_item_quantity_left"></div>
      <div class="input string required reconciliation_inventory_item_quantity_delivered"><label class="string required" for="reconciliation_inventory_item_quantity_delivered"><abbr title="required">*</abbr> Quantity delivered</label><input class="string required" type="text" name="reconciliation[inventory_item][quantity_delivered]" id="reconciliation_inventory_item_quantity_delivered"></div>
  </div>

我试过简单地添加:inventory_item到我的params.require(:reconciliation).permit(..),但这不起作用。

如果你想要permit inventory_item,你必须指定它的结构,因为它不是一个简单的字段,而是一个哈希:

def reconciliation_params
  params.require(:reconciliation).permit(:location_id, :display_id, inventory_item: [:id, :quantity_left, :quantity_delivered] )
end

通过查看您的日志,您没有传递inventory_item_id,这可能是更新此特定项目所需的:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"JZvhwloo0+XM9bmptxXGfnDw==", 
"reconciliation"=>{"location_id"=>"2", "inventory_item"=>
{"quantity_left"=>"1", "quantity_delivered"=>"170"}},
"commit"=>"Update", "location_id"=>"2"}

您可以将其添加为嵌套表单中的隐藏字段。

关联形式应为复数f.simple_fields_for :inventory_items 您应该在新控制器的操作中初始化新的inventory_item对象

def new
  @reconciliation = Reconciliation.new
  # you can create as many new items as you want
  @reconciliation.inventory_items.build
end

如果你想动态地向表单添加项目我建议你使用https://github.com/nathanvda/cocoon

但是看起来您想要将现有的inventory_item添加到新的对帐中,最好has_many through关联使用has_many through http://guides.rubyonrails.org/association_basics.html#choosing-between-has-many-through-and-has-and -belongs一对多

更容易添加具有必要字段和关联的连接模型对象。

另一个建议:如果你在这个局部使用实例变量,不要将局部变量发送到局部变量

# render partial
render 'form', reconciliation: @reconciliation
# partial with form for local variable
simple_form_for reconciliation

并且我认为你的表单部分不适用于编辑操作,因为硬编码的url,你可以在一个变量中传递url:

# new html
render 'form', reconciliation: @reconciliation, url_var: location_reconciliations(@location)
# edit
render 'form', reconciliation: @reconciliation, url_var: reconciliations(@reconciliation)
# form
simple_form_for reconciliation, url: url_var

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM