Two Association Label Elements in Simple Form

91 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 best solutions below

0
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.