簡體   English   中英

rails_admin用戶授權

[英]rails_admin user authorization

我是Rails的新手,尤其是rails_admin。 嘗試訪問管理頁面時,如何通知登錄前端的用戶沒有ADMIN? 謝謝。 這是index.html.erb

<p id="notice"><%= notice %></p><br>
<%= link_to "admin", rails_admin_path, class: "waves-effect waves-light btn" %>
    <h1>Listing Students</h1>
    <table class="hoverable">
     <thead>
        <tr>
          <th>Name</th>
          <th>Age</th>
          <th colspan="3"></th>
        </tr>
      </thead>
      <tbody>
        <% @students.each do |student| %>
          <tr>
            <td><%= student.name %></td>
           <td><%= student.age %></td>
          <td><%= link_to 'Show', student %></td>
       </tr>
    <% end %>
    </tbody>
   </table>

<br>

application.html.erb

<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>

應用控制器

class ApplicationController < ActionController::Base
  ...
  rescue_from CanCan::AccessDenied do |exception|
    redirect_to main_app.root_path, alert: exception.message
  end

app / models / ability.rb

class Ability
  include CanCan::Ability
  def initialize(user)
    user ||= User.new # guest user (not logged in)
    if user.roles.include? :admin
      can :manage, :all
    else

https://github.com/CanCanCommunity/cancancan

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM