Issues in getting UI in proper format using ruby on rails

AksharaDL

New Member
#1
I am working on a Ruby on rails project. I want to get the UI in the format as mentioned in the image below.

09yZH.jpg

I tried the below code, but not getting proper results. User can create controller dynamically from permission so the controller can be anything (User,Role,Permission etc)
Kindly give my any suggestions
Controller
Mã:
def display
  param = params[:role]
  id=param[:id]
  @permission = Role
                   .joins(:permissions)
                   .where('roles.id=?',id)
                   .select('permissions.*')

end
result of @permission

Mã:
[#<Role id: 4, name: "index", controller: "User", created_at: "2018-08-21
06:29:47", updated_at: "2018-08-24 06:39:37">,
#<Role id: 16, name: "create", controller: : "User", created_at: "2018-08-24
07:11:08", updated_at: "2018-08-24 07:11:08">,
#<Role id: 21, name: "destroy", controller: : "User", created_at: "2018-08-
24 07:31:17", updated_at: "2018-08-24 07:31:17">,
#<Role id: 25, name: "update", controller: : "User", created_at: "2018-08-24
07:52:00", updated_at: "2018-08-24 07:52:00">,
#<Role id: 26, name: "edit", controller: : "Role", created_at: "2018-08-27
06:38:39", updated_at: "2018-08-27 06:38:39">]>
view
Mã:
<%= link_to 'Create',  new_permission_path %>
<br/>
<thead>
<tr>
  <th width="25px"><%= "User" %></th>
 </tr>
 <tr>
 <% @permission.each do |p| %>
 <%  if (p.controller== 'User') %>
  <th width="25px"> <%= check_box_tag "p[]", p.id %> <%= p.name %></th>
 <% end %>
 <% end %>
 </tr>
 </thead>
 

Admin

Administrator
Thành viên BQT
#2
I think your structure should be like below

role model
Mã:
class Role < ApplicationRecord
  has_many :role_permissions
  has_many :permissions, through: :role_permissions, dependent: :destroy
end
permission model
Mã:
class Permission < ApplicationRecord
  has_many :role_permissions
  has_many :roles, through: :role_permissions, dependent: :destroy
end
and the middle table between role and permissions
Mã:
class RolePermission < ApplicationRecord
  belongs_to :role
  belongs_to :permission
end
Create data from seeds.rb
Mã:
 Role.create!(name: admin)
Permission.create!([
    {subject_class: 'Users', action: 'create',name: 'Create a User',description: 'nil', title: "Users"},
    {subject_class: 'Users', action: 'index',name: 'List users',description: 'nil', title: "Users"},
    {subject_class: 'Users', action: 'update',name: 'Update User',description: 'nil', title: "Users"},
    {subject_class: 'Users', action: 'destroy',name: 'Remove User',description: 'nil', title: "Users"},
    {subject_class: 'Users', action: 'show',name: 'Show  User',description: 'nil', title: "Users"},

    Role.first << Permission.all
Subject Class is name of controller and action will be name of action.

In the view you can load all role and permissions like below, I used accordion in my view, so you might need to update view as per your need
Mã:
 <% roles = Role.includes(:permissions).all %>                                   
 <% uniq_controller = Permission.all.group_by { |p| p.title } %>
            <div class="accordion panel-group" id="accordion2">
              <% uniq_controller.each do |permission| %>

                  <div class="panel panel-default">
                    <%= link_to "##{permission.first}" ,data: {parent: "#accordion2", toggle: "collapse"} do %>
                      <div class="panel-heading">
                          <h3 class="panel-title">
                             <%= permission.first.gsub("_"," ") %>
                          </h3>
                      </div>
                    <% end %>
                    <div id="<%= permission.first %>" class="panel-collapse collapse">
                      <div class="collapse show" role="tabpanel" aria-labelledby="headingOne">
                        <div class="card-block">
                          <table>
                            <% permission.second.each do |cont| %>


                                <tr>
                                  <td><%= cont.name %></td>
                                  <td>
                                    <%= f.check_box :permission_ids, {multiple: true}, cont.id, nil %></td>
                                </tr>

                            <% end %>
                          </table>
                        </div>
                      </div>
                    </div>
                  </div>
              <% end %>
            </div>
In the controller just add the permission_ids: [] in permit parameters
 
Top