5
votes

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

2
can you show your new action of the members_controller and the whole form?Vasiliy Ermolovich
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
Solved by using :to_label method. Thanks.Azren
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

2 Answers

9
votes

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.

5
votes

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