我遵循这个railscasthttp://railscasts.com/episodes/189-embedded-association?view=comments我认为它很棒,但我想处理的角色有点不同。
我希望有一个列'roles',它存储逗号分隔的值,例如:“administrator,teacher”,这样用户就分配了administrator和teacher角色。
我想设置一个复选框。
我现在拥有的保存了这个:"---\n- administrator\n- teacher\n- ''\n"
我怎么能那样做?
_form.html.erb
<div class="group">
<%= f.label :roles, "Roles", :class => :label %>
<% for role in User::ROLES %>
<%= check_box_tag "user[roles][]", role, @user.roles.include?(role) %>
<%=h role.humanize %><br />
<% end %>
<%= hidden_field_tag "user[roles][]", "" %>
</div>
user.rb
ROLES = %w[superadmin admin instructor salesperson student]
最佳答案
查看代码
下面应该有用
<% form_for @user do |f|%>
<div class="group">
<%= f.label :roles, "Roles", :class => :label %>
<% for role in User::ROLES %>
<%= f.check_box :roles, :name => "#{f.object_name}[roles][]", role%>
<%= h role.humanize %><br />
<% end %>
</div>
<% end %>
想了解更多信息
关于ruby-on-rails - Rails 3.使用复选框在一列中包含多个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9638296/