2014-02-05 25 views
0

我有一個使用slim/simple_form的表單。關聯模型的簡單表單label_method

= f.association :users, 
    collection: @users, 
    prompt: "Choose Recipient", 
    label_method: :first_name 

我想用戶的全名作爲標籤方法,有人可以指出我在正確的方向嗎?我只有first_name和last_name作爲屬性。爲了清晰起見,我還希望將用戶的公司名稱添加到其全名旁邊。簡單形式是否允許這樣做?不然,我就撤退到選項從集合選擇

回答

1

你的模型創建的方法:

class User < ArcitveRecord::Base 
    # ... 

    def full_name 
    "#{first_name} #{last_name}" 
    end 
end 

,並使用它:

= f.association :users, 
    collection: @users, 
    prompt: "Choose Recipient", 
    label_method: :full_name 
+0

我真的已經在模型中。傻我沒想到用它,我認爲只有數據庫屬性可用 –

1

如果你喜歡更喜歡旁邊添加company_namefull_name ,我只是修改Broisatse的答案爲

class User < ArcitveRecord::Base 
    # ... 

    def full_name_with_company_name 
    "#{first_name} #{last_name} #{company_name}" 
    end 
end 

並使用它:

= f.association :users, 
    collection: @users, 
    prompt: "Choose Recipient", 
    label_method: :full_name_with_company_name 
+0

好吧,謝謝:) –

+0

歡迎您:) – Pavan