开发者

Add further information to Rails Select list?

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 surve开发者_如何学Cyor 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


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}]"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜