【问题标题】:Admin Page Ruby on Rails管理页面 Ruby on Rails
【发布时间】:2015-11-04 18:51:25
【问题描述】:

我基于 Pundit 作为枚举创建了一个具有 3 个不同角色的网站,用户可以是分析师、开发人员或管理员。

我有一个控制器 System 有一个动作 users_list

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

下面是我的观点。 定义用户列表 @users = User.order(:id).page 参数[:page] 授权@users 结束

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

我想知道的是如何在此页面中启用管理员更改用户的角色。

【问题讨论】:

    标签: ruby-on-rails ruby devise pundit


    【解决方案1】:

    所以,我找到了我的问题的解决方案, 我在用户控制器上创建了一个新操作

        class UsersController < ApplicationController
    
      def update
        @user = User.find(params[:id])
        authorize @user
        if @user.update_attributes(secure_params)
          redirect_to controle_sistema_users_list_path, :success => 'User updated'
        else
          redirect_to controle_sistema_users_list_path, :alert => 'Unable to update user'
        end
      end
    
      private
      def secure_params
        params.require(:user).permit(:role)
      end
    end
    

    并在 users_list 页面的 td 元素内创建了一个表单

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

    【讨论】:

      猜你喜欢
      • 2016-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-11
      相关资源
      最近更新 更多