I'm building a Rails app for inventory tracking. When a user creates an inventory object, I want to create an associated inventory_condition object with a set of boolean values that describe different condition attributes.
# 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
In my form for creating an inventory object I'm having trouble passing true/false values through as params that the controller can use to build an inventory_condition object.
# _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>
In my inventories_controller I build my inventory object in association to its parent inventory_batch, and then I try to create the inventory_condition object, which is a belongs_to child of the inventory class.
# 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
When submitted, the condition parameters are:
":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"}
Which is not suitable to pass through as strong_params.
Two questions:
-
Is there a better way to store these condition attributes? I could put them right on each inventory object, but that didn't seem clean.
-
What's the proper way to create the checkbox fields and pass them through the
inventories_controllerto build myinventory_conditionobject?
Aucun commentaire:
Enregistrer un commentaire