0

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.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
DanielsV
  • 892
  • 1
  • 8
  • 26

1 Answers1

2
<%= 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.

Community
  • 1
  • 1
zmii
  • 4,123
  • 3
  • 40
  • 64