添加更多信息到 Rails 选择列表?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以向 Person 模型添加一个方法,该方法返回一个包含人员姓名和他/她工作的公司的字符串:
如果您不想在模型中包含此内容(因为这是与视图相关的代码),您可以, 您可以使用类似的方法,而不只是
在
Person.all.collect
调用中调用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:
If you do not want to have this in your model (because this is view related code), you can, instead of just calling
inside the
Person.all.collect
call, you can use something like