是否有可能创建一个
collection_select
or
select tag
for a
t.string
用户可以在哪里选择字符串的预定义值,并且只允许将那些值存储在字符串的数据库中?例如
t.string :relationship_status
我想要预定义的值:
In a relationship
Single
Maried
Engaged
ETC
The 可能有效的最简单的事情 http://c2.com/cgi/wiki?DoTheSimplestThingThatCouldPossiblyWork大致如下:
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)
这会产生:
<select name="person[relationship_status]">
<option value="single">single</option>
<option value="in a relationship">in a relationship</option>
...
</select>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)