2

I followed this railscast http://railscasts.com/episodes/189-embedded-association?view=comments which I think it's great but I want to handle the roles a bit differently.

I want to have a column 'roles' that stores comma delimited values, for example: "administrator, teacher" so that user has administrator and teacher roles assigned.

I would like to set that up having checkboxes.

What I currently have saves this instead: "---\n- administrator\n- teacher\n- ''\n"

How could I do that?

_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]

1 Answer 1

3

view code Below should work

<% 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 %>

for more info read something similar

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.