Two Association Label Elements in Simple Form

69 views Asked by At

How can I add two label elements when choosing association in Simple Form on Ruby on Rails?

Sample: @user.name = "Barack" and @user.last_name = "Obama"

Here is my code:

<%= f.association :persona, :collection => Persona.order(:name), 
:prompt => 'Choose a person' %> 

It displays only Barack but I need it to display not only name but also last_name when choosing from list.

1

There are 1 answers

0
zmii On BEST ANSWER
<%= f.association :persona, :collection => Persona.order(:name), :label_method => lambda { |persona| "#{persona.name} #{persona.last_name}" }, :prompt => 'Choose a person'%>

Here is the answer - you need a complex label_method.