添加更多信息到 Rails 选择列表?

发布于 2024-09-13 03:27:23 字数 1211 浏览 2 评论 0原文

我的 Rails 应用程序中有一个选择列表下拉菜单,采用用于创建新记录的表单。

<li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li>

这样用户就可以从测量员姓名列表中选择他们想要将哪一个与他们正在创建的案例相关联。

我想做的是将测量员的公司名称列在他们的名字旁边的括号中。

目前代码如下所示:

<li>Appointed Company<span><%= f.select :appointedsurveyor_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => (@kase.appointedsurveyor_id rescue "")} %></span></li>
<li>Appointed Surveyor<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li>

是否可以在测量员的名字后面输出测量员工作的公司?

关联如下:

class Company < ActiveRecord::Base
  has_many :kases
  has_many :people

class Person < ActiveRecord::Base
  has_many :kases # foreign key in join table
  belongs_to :company

我还更改了将公司记录输出为他们的姓名,而不是他们的 ID 号。

def to_s; companyname; end

提前致谢!

谢谢,

丹尼

I have a select list drop down in my Rails application, in the form for creating a new record.

<li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li>

This is so the user can choose from a list of Surveyor's names which one they want to associate with the Case they are creating.

What I would like to do is have the company name of the Surveyor listed next to their name in brackets.

At the moment the code looks like this:

<li>Appointed Company<span><%= f.select :appointedsurveyor_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => (@kase.appointedsurveyor_id rescue "")} %></span></li>
<li>Appointed Surveyor<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li>

Is it possible to have the company that a surveyor works for outputting after their name?

The associations are as follows:

class Company < ActiveRecord::Base
  has_many :kases
  has_many :people

class Person < ActiveRecord::Base
  has_many :kases # foreign key in join table
  belongs_to :company

I have also changed to output of the company records to their names rather than their ID numbers'.

def to_s; companyname; end

Thanks in advance!

Thanks,

Danny

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

总攻大人 2024-09-20 03:27:23

您可以向 Person 模型添加一个方法,该方法返回一个包含人员姓名和他/她工作的公司的字符串:

def name_and_company
  return "#{name} [#{company.name}]"
end

如果您不想在模型中包含此内容(因为这是与视图相关的代码),您可以, 您可以使用类似的方法,而不只是

x.personname

Person.all.collect 调用中调用

"#{name} [#{company.name}]"

You can add a method to your Person model that returns a string containing the persons name and the Company he/she is working for:

def name_and_company
  return "#{name} [#{company.name}]"
end

If you do not want to have this in your model (because this is view related code), you can, instead of just calling

x.personname

inside the Person.all.collect call, you can use something like

"#{name} [#{company.name}]"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文