gpt4 book ai didi

ruby-on-rails - Rails 5 集合选择

转载 作者:数据小太阳 更新时间:2023-10-29 08:30:34 26 4
gpt4 key购买 nike

我正在尝试创建一个 collection_select 下拉列表,其中包含另一个模型的字段值。我得到了以下 2 个模型:

文档:

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

条目:

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。

最佳答案

class Document < ApplicationRecord
has_many :entries
end


class Entry < ApplicationRecord
belongs_to :document
end

在你的 View 文件中,如:new.html.erb

 <%= f.select :document_id, Document.all.collect { |p| p.id }, include_blank: true %>

关于ruby-on-rails - Rails 5 集合选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38912892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com