2012-04-09 93 views
4

我正在尝试使用simple_form为“成员”创建表单,并且在显示组织名称时显示组织名称时出现问题,而不是显示id或organization_name。我在这里错过了什么吗?我应该怎么做呢?Rails simple_form association

**组织:0x0000000485cf88

组织:0x0000000485c948

组织: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 %> 

感谢。

干杯, Azren

+0

你能证明你的''的和members_controller'整个表单的new'行动? – 2012-04-09 19:57:40

+0

看起来像组织模型没有任何这些字段:'[:to_label,:name,:title,:to_s]'所以'SimpleForm'不能检测默认的标签和值的方法进行收集。我认为你应该手动传递它。 – 2012-04-09 20:05:21

+0

使用to_label方法解决。谢谢。 – Azren 2012-04-10 05:49:09

回答

8

看起来像Organization模型没有任何一个字段:[ :to_label, :name, :title, :to_s ]所以SimpleForm不能检测到集合的默认标签和值的方法。我认为你应该手动传递它。

3

添加to_label功能,以您的组织类如下图所示

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