Admin View Ruby on Rails

1

Next I created a site using Ruby on Rails and the Devise and Pundit gems. Users have a role that can be Analyst, Developer or Administrator.

I am using a View to list all users registered to the admin.

This is my Controller :

def users_list
  @users = User.order(:id).page params[:page]
  authorize @users
end

This is my View :

<%- model_class = User -%>
<div class="page-header">
  <h1><%=t '.title', :default => model_class.model_name.human.pluralize.titleize %></h1>
</div>
<table class="table table-striped">
  <thead>
    <tr>
      <th><%= model_class.human_attribute_name(:id) %></th>
      <th><%= model_class.human_attribute_name(:email) %></th>
      <th><%= model_class.human_attribute_name(:role) %></th>
      <th><%= model_class.human_attribute_name(:created_at) %></th>
      <th><%=t '.actions', :default => t("helpers.actions") %></th>
    </tr>
  </thead>
  <tbody>
    <% @users.each do |user| %>
      <tr>
        <td><%= link_to user.id, edit_user_registration_path(user) %></td>
        <td><%= user.email %></td>
        <td><%= user.role %></td>
        <td><%=l user.created_at %></td>
        <td>

          <%= link_to t('.destroy', :default => t("helpers.links.destroy")),
                      cancel_user_registration_path(user),
                      :method => :delete,
                      :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
                      :class => 'btn btn-xs btn-danger' %>
        </td>
      </tr>
    <% end %>
  </tbody>
</table>

<%= link_to t('.new', :default => t("helpers.links.new")),
            new_user_registration_path,
            :class => 'btn btn-primary' %>

How could I do to make it possible to change the Role of the user in this same View list of users?

    
asked by anonymous 13.11.2015 / 13:28

1 answer

0

Inside a td element I added and modified the action update in the UsersController to accept the parameter: role

<td>
 <%= form_for(user) do |f| %>
   <%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %>
   <%= f.submit 'Change Role', :class => "btn btn-default btn-xs" %>
 <%end%>
</td>
    
18.11.2015 / 18:18