39

I have been struggling with what I perceive to be a simple problem:

Working in Rails 3.0.8 with the simple_form 1.4 gem.

I have two models, owners and owner_types;

class Owner < ActiveRecord::Base
  belongs_to :own_type
  attr_accessible :name, :own_type_id
end

class OwnerType < ActiveRecord::Base
  has_many :owners
  attr_accessible :name, :subtype_name
end

In my the _form partial of the Owner view, I want to have a select box that displays both the name and subtype_name of the owner_type association.
....something like this: Owner Type: [name | subtype_name] eg. [Government | Federal]; [Government | Municipal]

My view now contains: app/views/owners/_form.html.erb

<%= simple_form_for @owner do |f| %>
  <%= f.error_messages %>
  <%= f.input :name %>
  <%= f.association :owner_type, :include_blank => false %>
  <%= f.button :submit %>
<% end %>

...the f.association only list the owner_type.name field by default. How do you specify different fields, or in my case two fields?

All help is appreciated; thanks in advance.

DJ

lulalala
  • 17,572
  • 15
  • 110
  • 169
dj_44
  • 395
  • 1
  • 3
  • 5

2 Answers2

91

You'll have to use the :label_method option for this.

<%= f.association :owner_type, :include_blank => false, :label_method => lambda { |owner| "#{owner.name} | #{owner.subtype_name}" } %>

or, if you define a select_label method on the owner's class, you can do

<%= f.association :owner_type, :include_blank => false, :label_method => :select_label %>
Dogbert
  • 212,659
  • 41
  • 396
  • 397
  • 1
    thanks for the prompt reply. It worked like a charm. If I could ask for an explanation: why, in the label_method, did you have owner, and owner.name; when in fact the values are located in the owner_type model (I would think it would have been ...lambda { |owner_type| "#{owner_type.name | #{owner_type.subtype_name}"} – dj_44 Jun 13 '11 at 19:00
  • @dj_44: Ah, I thought it was the owner model. You can use any parameter name for lambdas, `lambda { |a| a * 2 }` is the same as `lambda { |b| b * 2 }`, just like functions. – Dogbert Jun 13 '11 at 19:04
  • actually, both owner and owner_type appear to work the same. cross-post ...thanks again – dj_44 Jun 13 '11 at 19:04
  • In a related question, how on the show.html.erb page would I that same lambda values to appear instead of the owner_type_id. Or, probably more likley my owners_controller show method which is currently def show @owner = Owner.find(params[:id]) end – dj_44 Jun 13 '11 at 19:45
  • @dj_44: Could you post a link to the contents of your show.html.erb? – Dogbert Jun 13 '11 at 22:07
54

The easiest way to do this is implement an method to_label on your Model. Like this:

class OwnerType < ActiveRecord::Base
  def to_label
    "#{name} | #{subtype_name}"
  end
end

SimpleForm by default will search fot this methods on your model and use it as label_method, in this order:

:to_label, :name, :title, :to_s

You can also change this option on your simple_form.rb initializer, or you can pass a block or a method to :label_method option of your input.

Ted
  • 7,122
  • 9
  • 50
  • 76
rafaelfranca
  • 3,285
  • 24
  • 15