【发布时间】:2015-10-08 06:51:17
【问题描述】:
我正在构建一个用于库存跟踪的 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 对象的参数传递。
# _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_batch 关联的inventory 对象,然后我尝试创建inventory_condition 对象,它是inventory 类的belongs_to 子级。
# 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"}
【问题讨论】:
标签: ruby-on-rails checkbox associations