2013-10-10 44 views
-1

我在“查看”输入集合(simple_form),但该集合应该在模型中。ruby​​,simple_form Rails,collection

我必须声明其含有[“兄弟”,“女儿”,“父亲”,“朋友”,“丈夫”,“母亲”,“姐妹”,“儿子”,“妻子”]中的变量模型和考虑在这里使用:

= f.input :relationship 

这是我的观点:

= simple_form_for @emergency_information, html: {class: 'form-horizontal' } do |f| 
    = f.error_notification 
    = f.input :name 
    = f.input :relationship, collection: ["Brother", "Daughter", "Father", "Friend", "Husband", "Mother", "Sister", "Son", "Wife"] 

这是我的模型

class EmergencyInformation < ActiveRecord::Base 
    belongs_to :user 

    validates :user_id, :name, presence: true 

end 

请帮助我!

+2

我不确定我是否理解这个问题。或者如果有的话。 – sevenseacat

+0

我编辑了这个问题。 –

+0

您的'emergency_information'表中是否包含名为'relationship'的属性? – lurker

回答

1

如果我理解正确你的努力是找出在哪里附加这个数组。通常在这些情况下,我将它作为常量添加到我的模型中,并将它用于列表值并验证提交的值是否来自数组。

class EmergencyInformation < ActiveRecord::Base 
    RELATIONSHIPS_TYPES = ["Brother", "Daughter", "Father", "Friend", "Husband", "Mother", "Sister", "Son", "Wife"] 
    belongs_to :user 

    validates :user_id, :name, presence: true 
    validates :relationship, inclusion: RELATIONSHIPS_TYPES 
end 

# in view 
= f.input :relationship, collection: EmergencyInformation::RELATIONSHIPS_TYPES 

或者您可以提取此数组以分离服务对象,但在这种情况下,感觉就像过度工作。

+0

TNX!而已! –