1

I have 2 scaffolds Role(name:string), Permission(name:string) I created a relation between them. So now i can display Role's permissions in an easy way: Role.permissions.

The problem is to create a form for new Role, because in addition to the field 'name' I would like to add there a list of permissions (checkboxes).

This is what I managed to do so far. (Now I'm using a textbox instead of checkbox)

<%= form_for(@role)  do |f| %>
  <% if @role.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@role.errors.count, "error") %> prohibited this role from being saved:</h2>

      <ul>
      <% @role.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>

  <div class="list_of_fields">
    <div class="field">
      <%= text_field_tag :myVariable] %>
    </div>
  </div>

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

Precisely I'm talking about this piece of code:

  <div class="list_of_fields">
    <div class="field">
      <%= text_field_tag :myVariable] %>
    </div>
  </div>

This produces:

Parameters: {"utf8"=>"â", "authenticity_token"=>"MdS0VkLAxGoZM8P71H0a9M54CpMt8DnnwSp75UVGtqE=", "role"= {"name"=>"abc"}, "myVariable"=>"xyz", "commit"=>"Create Role"}

I don't like this solution because if somebody chooses a specific name of permission it may fail ('commit' e.g.).

I would like to close all these additional fields within a hash. What's the right way to do this?

1 Answer 1

2

Have a look at Railscast 189 - embedded association. Ryan tackles exactly what you're looking to do here. It's a bit old and uses roles embedded in the user model, but you should be able to adapt it.

Additionally, you might consider using cancan for roles based permissions (railscast here)

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.