我正在为库存跟踪构建Rails应用程序。当用户创建inventory
对象时,我想创建一个关联的inventory_condition
对象,其中包含一组描述不同条件属性的布尔值。
# schema.rb
create_table "inventories", force: true do |t|
t.integer "tradein_id"
t.string "esn"
t.float "price_paid"
t.string "erase_method"
t.string "status"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "device_id"
t.string "resale_reference"
t.integer "condition_id"
t.integer "inventory_batch_id"
end
create_table "inventory_conditions", force: true do |t|
t.string "grade"
t.boolean "bad_esn", default: false, null: false
t.boolean "find_my_iphone_locked", default: false, null: false
t.boolean "broken_glass", default: false, null: false
t.boolean "broken_lcd", default: false, null: false
t.boolean "broken_charge_port", default: false, null: false
t.boolean "broken_buttons", default: false, null: false
t.boolean "broken_speaker_microphone", default: false, null: false
t.boolean "broken_camera", default: false, null: false
t.boolean "broken_wifi", default: false, null: false
t.boolean "broken_cellular", default: false, null: false
t.integer "inventory_id"
end
在我创建inventory
对象的表单中,我无法通过控制器可用于构建inventory_condition
对象的参数传递true / false值。
# _form.html.erb (inventory)
<div class="form-group">
<%= f.label :functional_condition %><br>
<% @conditions.each do |x| %>
<div class="checkbox">
<label>
<%= check_box "[:condition]", ":#{x}", {}, "true", "false"%> <%= x.humanize.split.map(&:capitalize).join(' ') %>
</label>
</div>
<% end %>
</div>
在我的inventories_controller
中,我将inventory
对象与其父inventory_batch
建立关联,然后我尝试创建inventory_condition
对象,该对象是belongs_to的子对象inventory
班。
# inventories_controller.rb
def create
@inventory_batch = InventoryBatch.find(params[:inventory_batch_id])
@inventory = @inventory_batch.inventories.build(inventory_params)
@condition = @inventory.inventory_conditions.build(inventory_conditions_params)
end
提交时,条件参数为:
":condition"=>{":bad_esn"=>"false", ":find_my_iphone_locked"=>"false", ":broken_glass"=>"true", ":broken_lcd"=>"true", ":broken_charge_port"=>"false", ":broken_buttons"=>"false", ":broken_speaker_microphone"=>"false", ":broken_camera"=>"false", ":broken_wifi"=>"false", ":broken_cellular"=>"false"}
不适合以strong_params
传递。
两个问题:
有没有更好的方法来存储这些条件属性?我可以把它们放在每个库存对象上,但这看起来并不干净。
创建复选框字段并通过inventories_controller
传递它们以构建我的inventory_condition
对象的正确方法是什么?
修改 - 在提交表单时添加完整参数
Parameters:
{"utf8"=>"✓",
"authenticity_token"=>"MWkzncKzxhsqn5DmyA3nRblCrTfTz3EgnF8BSKFWfjs=",
"inventory"=>{"esn"=>"",
"price_paid"=>"",
"erase_method"=>"Pre-Erased",
"inventory_conditions"=>{"bad_esn"=>"false",
"find_my_iphone_locked"=>"false",
"broken_glass"=>"false",
"broken_lcd"=>"true",
"broken_charge_port"=>"true",
"broken_buttons"=>"false",
"broken_speaker_microphone"=>"false",
"broken_camera"=>"false",
"broken_wifi"=>"false",
"broken_cellular"=>"false"}},
"Manufacturer"=>"",
"Network"=>"Select the Network",
"Model"=>"Select the Model",
"Variant"=>"Select the Variant",
"Capacity"=>"Select the Capacity",
"Color"=>"Select the Color",
"condition"=>"",
"commit"=>"Save Inventory",
"inventory_batch_id"=>"16"}
答案 0 :(得分:0)
使用fields_for
方法:
# _form.html.erb (inventory)
<div class="form-group">
<%= f.label :functional_condition %><br>
<%= f.fields_for :inventory_conditions do |i| %>
<% @conditions.each do |x| %>
<div class="checkbox">
<label>
<%= i.check_box "[:condition]", "#{x}".to_sym %> <%= x.humanize.split.map(&:capitalize).join(' ') %>
</label>
</div>
<% end %>
<% end %>
</div>
编辑(这个东西适用于单一的强params方法):
这样,inventory_conditions
参数作为inventories
参数的子哈希传递。您需要更新强大的参数以包含此内容:
inventory_conditions_attributes: [:broken_glass, :broken_lcd, etc...]
这必须在你强大的参数列表的末尾(即esn
之后,status
等)。