【问题标题】:Creating an associated object of true/false values in Rails with check_box使用 check_box 在 Rails 中创建真/假值的关联对象
【发布时间】: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传递。

两个问题:

  1. 有没有更好的方法来存储这些条件属性?我可以将它们放在每个库存对象上,但这似乎并不干净。

  2. 创建复选框字段并将它们传递给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


    【解决方案1】:

    使用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>
    

    编辑(这个东西是针对单一强参数方法的):

    这样,inventory_conditions 参数作为inventories 参数的子哈希传递。您需要更新您的强参数以包含以下内容:

    inventory_conditions_attributes: [:broken_glass, :broken_lcd, etc...]
    

    这必须位于强参数列表的末尾(即在esnstatus 等之后)。

    【讨论】:

    • 谢谢!当我在控制器中构建新的inventory_condition 时,如何访问这些属性?像这样? @inventory.inventory_conditions.build(inventory_params[inventory_conditions_attributes])
    • 如果库存has_manyinventory_conditions,是的。如果是has_one,那么就是@inventory.build_inventory_conditions(...)
    • 现在我在提交表单时收到undefined local variable or method inventory_conditions_att‌​ributes for #&lt;InventoriesController:0x007f84c267d740&gt;
    • 您能否更新您的问题以包含提交表单时提交的参数?我可能拼错了inventory_conditions_att‌​ributes
    • 另外,现在如果我选中了任何复选框,我会收到unknown attribute: inventory_conditions,它会在线上调用我的inventory 对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多