5

I'm trying to create a form for 'Member' using simple_form and having trouble displaying an association where it display the organization as below instead of the id, or organization_name. Am I missing something here? How should I go about this?

**Organization:0x0000000485cf88

Organization:0x0000000485c948

Organization:0x0000000485c358**

class Organization < ActiveRecord::Base
  has_many :members
  attr_accessible :organization_name
end

class Member < ActiveRecord::Base
  belongs_to :organization
  attr_accessible :active, :email, :first_name, :last_name, :role
end

  <%= f.input :first_name %>
  <%= f.input :last_name %>
  <%= f.input :role %>
  <%= f.input :email %>
  <%= f.input :active %>
  <%= f.association :organization %>

  <%= f.button :submit %>

Thanks.

Cheers, Azren

Azren
  • 227
  • 1
  • 4
  • 16
  • can you show your `new` action of the `members_controller` and the whole form? – Vasiliy Ermolovich Apr 09 '12 at 19:57
  • looks like Organization model doesn't have any of these fields: `[ :to_label, :name, :title, :to_s ]` so `SimpleForm` can't detect a default label and value methods for collection. I think you should pass it manually. – Vasiliy Ermolovich Apr 09 '12 at 20:05
  • Solved by using :to_label method. Thanks. – Azren Apr 10 '12 at 05:49
  • By the way for the record, I used organization:references in my Members migration and if I just use organization_id:integer it will be what I was expecting using the f.association :organization. Anyway thanks guys for your help. Appreciate that. – Azren Apr 10 '12 at 05:52

2 Answers2

9

looks like Organization model doesn't have any of these fields: [ :to_label, :name, :title, :to_s ] so SimpleForm can't detect a default label and value methods for collection. I think you should pass it manually.

Vasiliy Ermolovich
  • 24,459
  • 5
  • 79
  • 77
5

add to_label function to your Organization class as shown below

class Organization < ActiveRecord::Base
  has_many :members
  attr_accessible :organization_name

  def to_label
    "#{organization_name}"

  end
end

refered Simple form association custom label name

Community
  • 1
  • 1
John
  • 161
  • 2
  • 3