2014-02-05 27 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