2

collection_select別のモデルのフィールドの値をドロップダウンするようにしようとしています。私は次の2つのモデルを手に入れました:

Documents:

class CreateDocuments < ActiveRecord::Migration[5.0]
  def change
    create_table :documents do |t|
      t.string :etiquette_number
      t.string :etiquette_type
      t.boolean :important
      t.string :work_text
      t.integer :user_id


      t.timestamps
    end
  end
end

Entries:

class CreateEntries < ActiveRecord::Migration[5.0]
  def change
    create_table :entries do |t|
      t.integer :document_id
      t.integer :user_id
      t.string :work
      t.date :date
      t.integer :time

      t.timestamps
    end
  end
end

ドキュメントのIDの値を選択できるドロップダウン選択をdocument_id(モデル内で)取得したい。Entries

私はこれまでこれを手に入れましたが、それが正しい方法であるかどうかはわかりません

models/document.rb

class Document < ApplicationRecord
  has_many :Entries
end

models/entry.rb

class Entry < ApplicationRecord
  belongs_to :Documents
end

誰かが私を助けてくれることを本当に願っています。タイトルにあるように、私は Rails 5 を使用しています。

4

3 に答える 3

5
class Document < ApplicationRecord
 has_many :entries
end


class Entry < ApplicationRecord
 belongs_to :document
end

次のようなビューファイルで: new.html.erb

 <%= f.select :document_id, Document.all.collect { |p| p.id }, include_blank: true %>
于 2016-08-12T08:31:24.517 に答える