是否可以使用 Collection_Select 从参考模型中为同一表单中的多个变量生成相同的下拉列表?

发布于 2024-11-30 09:50:50 字数 1047 浏览 0 评论 0原文

我认为这个问题的第一个版本太复杂,并且证明了解决问题的错误尝试。考虑这个假设的迁移文件中的模型:

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

该程序有必要:

  1. 允许用户从制造商的下拉列表中为 oem1、oem2 和 oem3 分配值。
  2. 允许管理员或高级用户向用户将从中选择的列表添加或删除值。

通过创建一个像此迁移文件中所示的模型可以轻松实现第二个目标:

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:

  1. Allow the user to assign values to oem1, oem2 and oem3 from a drop-down list of manufacturers.
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

横笛休吹塞上声 2024-12-07 09:50:50

在您的表单中,您可以执行以下操作:

<%= form_for @project do |f| %>
  <p>
    <%= f.label :oem1 %>
    <%= f.collection_select :oem1, Oem.all, :name, :name %>
  </p>
  <p>
    <%= f.label :oem2 %>
    <%= f.collection_select :oem2, Oem.all, :name, :name %>
  </p>
  <p>
    <%= f.label :oem3 %>
    <%= f.collection_select :oem3, Oem.all, :name, :name %>
  </p>

  <!-- The rest of your form... -->
<% end -%>

不确定您的要求,但听起来项目和 OEM 之间的关系是多对多。那么,使用 has_and_belongs_to_many 怎么样?

In your form you can do the following:

<%= form_for @project do |f| %>
  <p>
    <%= f.label :oem1 %>
    <%= f.collection_select :oem1, Oem.all, :name, :name %>
  </p>
  <p>
    <%= f.label :oem2 %>
    <%= f.collection_select :oem2, Oem.all, :name, :name %>
  </p>
  <p>
    <%= f.label :oem3 %>
    <%= f.collection_select :oem3, Oem.all, :name, :name %>
  </p>

  <!-- The rest of your form... -->
<% end -%>

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?

蓝色星空 2024-12-07 09:50:50

我想我可能误解了你的意思,但你是说这行不通吗?

- oems = Oem.all

= collection_select(:project, :oem1, oems, :id, :name, :prompt => true)
...
= collection_select(:project, :oem2, oems, :id, :name, :prompt => true)
...
= collection_select(:project, :oem3, oems, :id, :name, :prompt => true)

I think I may be misunderstanding you, but are you saying this won't work?

- oems = Oem.all

= collection_select(:project, :oem1, oems, :id, :name, :prompt => true)
...
= collection_select(:project, :oem2, oems, :id, :name, :prompt => true)
...
= collection_select(:project, :oem3, oems, :id, :name, :prompt => true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文