2013-10-10 77 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!而已! –