gpt4 book ai didi

ruby-on-rails - Rails 5.1 中的未知参数强参数问题

转载 作者:行者123 更新时间:2023-12-04 14:54:27 25 4
gpt4 key购买 nike

所以我的 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>

最佳答案

I have tried simply adding :inventory_item to my params.require(:reconciliation).permit(..), but that doesn't work.



如果要permitinventory_item,必须指定它的结构,因为它不是一个简单的字段,而是一个hash:
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"}

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

关于ruby-on-rails - Rails 5.1 中的未知参数强参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49472536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com