2015-08-22 143 views
0

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

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

分享你的控制器,這是使用[flash消息]完成的地方(http://api.rubyonrails.org/classes/ActionDispatch/Flash.html) –

回答

0

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 

應用程序/模型/ 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