Question

I have a select box like so:

  <div class="form-group">
    <label class="col-sm-2 control-label'">Select permissions for this role</label>
    <div class="col-sm-6">
      <%= f.select(:permissions, options_for_select(Xaaron::Permission.pluck(:permission),
        :selected => @role.permissions), {}, class: 'form-control', multiple: true) %>
    </div>
  </div>

This select box allows me to select multiple permissions and assign them to a role. which is great and all accept that when I use the same select box on the edit form, all the fields for that role are filled in but the select box is left blank.

The database shows a relationship between role and permission, but wont show the permissions selected. Am I missing something here?

Was it helpful?

Solution

What is the type of your permission attribute of Xaaron::Permission model? If it is a reference (integer) you could try this:

<%= f.select(:permissions, options_for_select(Xaaron::Permission.pluck(:name, :id), 
    @role.permissions.pluck( :id ), {}, class: 'form-control', multiple: true) %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top