简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM