是否可以使用 Collection_Select 从参考模型中为同一表单中的多个变量生成相同的下拉列表?
我认为这个问题的第一个版本太复杂,并且证明了解决问题的错误尝试。考虑这个假设的迁移文件中的模型:
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.references :oem1
t.references :oem2
t.references :oem3
t.timestamps
end
end
def self.down
drop_table :projects
end
end
该程序有必要:
- 允许用户从制造商的下拉列表中为 oem1、oem2 和 oem3 分配值。
- 允许管理员或高级用户向用户将从中选择的列表添加或删除值。
通过创建一个像此迁移文件中所示的模型可以轻松实现第二个目标:
class CreateOems < ActiveRecord::Migration
def self.up
create_table :oems do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :projects
end
end
如果我的 PROJECT 模型中没有三个唯一的 OEM 变量,我只需创建 PROJECT :belongs_to oem 和 < strong>OEM:has_many 项目,只需使用 collection_select 从 OEM 模型中选择 OEM 名称,并将其分配给 PROJECT 模型中的 OEM 引用变量。但是,我有三个 OEM 变量,它们不能全部命名为 OEM。因此,据我所知,我无法将所有这三个变量链接到 collection_select 下拉列表中的 OEM.name 变量。
那么,我该怎么办?还有其他方法可以实现这两个目标吗?
I think my first version of this question was too complicated and demonstrated an incorrect attempt at solving the problem. Consider the model in this hypothetical migration file:
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.references :oem1
t.references :oem2
t.references :oem3
t.timestamps
end
end
def self.down
drop_table :projects
end
end
It is necessary for this program to:
- Allow the user to assign values to oem1, oem2 and oem3 from a drop-down list of manufacturers.
- Allow an Admin or Power User to add or remove values to the list from which the user(s) will select.
The second objective is easily achieved by creating a model like the one shown in this migration file:
class CreateOems < ActiveRecord::Migration
def self.up
create_table :oems do |t|
t.string :name
t.timestamps
end
end
def self.down
drop_table :projects
end
end
If I didn't have three unique OEM variables in the PROJECTs model I would just make PROJECT :belongs_to oems and OEM :has_many projects and just use collection_select to select an OEM name from the OEMs model and assign it to an OEM reference variable in the PROJECTs model. However, I have three OEM variables and they cannot all be named OEM. So, as far as I know, I cannot link all three of these variables to the OEM.name variable in a collection_select drop-down list.
So, what do I do? Is there another way to accomplish those two objectives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的表单中,您可以执行以下操作:
不确定您的要求,但听起来项目和 OEM 之间的关系是多对多。那么,使用
has_and_belongs_to_many
怎么样?In your form you can do the following:
Not sure about your requirements, but it sounds like the relationship between projects and oems is many-to-many. So, how about using
has_and_belongs_to_many
?我想我可能误解了你的意思,但你是说这行不通吗?
I think I may be misunderstanding you, but are you saying this won't work?