Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails simple_form association

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

like image 966
Azren Avatar asked Apr 09 '12 03:04

Azren


2 Answers

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.

like image 129
Vasiliy Ermolovich Avatar answered Sep 17 '22 23:09

Vasiliy Ermolovich


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

like image 45
John Avatar answered Sep 20 '22 23:09

John