在t.string的预定义值中选择

有可能创建一个

collection_select 

要么

 select tag 

为一个

 t.string 

用户可以在字符串的预定义值中进行选择,并且只允许那些值存储在字符串的数据库中? 例如

 t.string :relationship_status 

我想要预定义的值:

 In a relationship Single Maried Engaged ETC 

可能工作最简单的事情是:

 class Person < ActiveRecord::Base RELATIONSHIP_STATUSES = [ "single", "in a relationship", "together", "it's complicated" ] validates :relationship_status, :inclusion => RELATIONSHIP_STATUSES end 

然后,在视图中:

 collection_select(:person, :relationship_status, Person::RELATIONSHIP_STATUSES, :to_s) 

这会产生: