开发者

Rails select form helper needs to save multiple attribute values of one object

开发者 https://www.devze.com 2023-04-08 11:32 出处:网络
I have a form where I\'m creatin开发者_高级运维g an interview for a job posting and one of the fields is a dropdown where I select the candidate (from a list of candidates created on another page) tha

I have a form where I'm creatin开发者_高级运维g an interview for a job posting and one of the fields is a dropdown where I select the candidate (from a list of candidates created on another page) that I plan to interview.

So far I'm able to save the details of the interview as well as the candidates name, but I also want to save the candidates id. I'm not sure how to go about this.

Currently I have

= select("interview", "interviewee_name", Candidate.order('last_name ASC').collect {|c| [c.fullname]})

Somehow I need to find a way to retrieve and save the correct value to candidate_id.

Any help is appreciated :)


= select("interview", "interviewee_name", Candidate.order('last_name ASC').collect{|c| [c.fullname, c.id]})

UPD

Then (if you really need to store its name) in model add callback:

class Interview < AR::Base
  before_save :set_candidate_name
  belongs_to :candidate

private

  def set_candidate_name
    self.interviewee_name = self.candidate.fullname
  end
end


You probably need to use javascript to do this - basically put a click handler on the select element and make an ajax call to the server to retrieve the candidate name from the id. Then populate the name field in the callback.

I think I have some sample code somewhere if you are using jquery ...

0

精彩评论

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

关注公众号