从 rails 中的另一个 table 创建下拉列表

Creating a drop down list from another table in rails

我正在尝试向现有 table 添加关系字段下拉列表 select。我当前的架构

create_table "hardwares", force: :cascade do |t|
  t.string "serialnumber"
  t.string "modelnumber"
  t.string "modeltype"
  t.string "location"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.integer "poc_id"
  t.index ["poc_id"], name: "index_hardwares_on_poc_id"
end

create_table "pocs", force: :cascade do |t|
  t.string "name"
  t.string "address"
  t.string "facility"
  t.string "phone"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

硬件 table 中的索引字段是在我 运行 迁移时创建的。

rails g migration addPocReferencesToHardwares poc:references 

它生成了下面的迁移文件以及架构中的索引

class AddPocReferencesToHardwares < ActiveRecord::Migration[5.1]
  def change
    add_reference :hardwares, :poc, foreign_key: true
  end
end

建立关系后,我希望能够 select 硬件的 POC 按名称作为所有可用 POC 的下拉列表。

我将其添加到硬件表格中:

<div class="field">
  <%= form.label "POC" %>
  <%= form.collection_select(:poc_id, Poc.all, :id, :name, { :prompt => 'Select a POC', :selected => @poc.poc_id }, { class: 'form-control' }) %>
</div>

我得到的错误是“nil:NilClass 的未定义方法 `poc_id'”。如何允许下拉 selection 向硬件添加 POC?

问题是我将关系设置为 POC 属于硬件,因此在表单字段中它需要是:

<div class="field">
  <%= form.label "POC" %>
  <%= form.collection_select(:poc_id, Poc.all, :id, :name, { :prompt => 'Select a POC', :selected => @hardware.poc_id }, { class: 'form-control' }) %>
</div>