我的数据库中有一个字符串列,它是“artist”或“listener”,我希望用户能够通过单击相应的复选框来选择填充列的字符串。我该怎么做?
答案 0 :(得分:2)
你应该在这里使用单选按钮:
# Imagine it is User model and user_type field
<%= form_for User.new do |f| %>
<%= f.radio_button :user_type, "artist" %>
<%= f.radio_button :user_type, "listener" %>
<% end %>
答案 1 :(得分:1)
f.check_box :my_field, {}, "artist", "listener"
这会使my_field
在检查时成为“艺术家”,在未选中时成为“聆听者”。
答案 2 :(得分:1)
你应该使用单选按钮。还要确保将该逻辑放入模型中(验证)。
# model
class User
TYPES = %w(artist listener)
validates_inclusion_of :user_type, :in => TYPES
end
# view
<%= form_for :user do |f| %>
<% User::TYPES.each do |type| %>
<%= f.radio_button :user_type, type %>
<% end %>
<% end %>