【发布时间】:2016-12-19 04:02:37
【问题描述】:
我正在尝试制作一个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
我想在document_id(Entries 模型中)上获得一个下拉选择,我可以在其中选择文档的 id 值。
到目前为止我得到了这个,但我不确定它是否是正确的方法
models/document.rb
class Document < ApplicationRecord
has_many :Entries
end
models/entry.rb
class Entry < ApplicationRecord
belongs_to :Documents
end
我真的希望有人可以帮助我,正如您在标题中看到的那样,我正在使用 Rails 5。
【问题讨论】:
-
试试
has_many :entries和belongs_to :documents -
但我将模型命名为条目和文档。还是 :entries 和 :documents 吗?
-
好的,谢谢,我认为 collection_select apidock.com/rails/ActionView/Helpers/FormOptionsHelper/… 是可能的,但不知道如何
-
我想在前端显示那个下拉菜单。对不起,我忘了写这个
标签: ruby-on-rails ruby ruby-on-rails-5 collection-select