2012-04-09 101 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