使用 CanCan 和 Devise 时未经授权 (Rails 4)
Not Authorized when using CanCan and Devise (Rails 4)
我正尝试在自定义路线上提供一些资源,但通过我的 CanCan 访问被拒绝 gem。
具有自定义路由的 PlaysController:upcoming_plays
class PlaysController < ApplicationController
load_and_authorize_resource
def upcoming_plays
@plays = Play.where("date_of_play >= ?",Time.now ).order(date_of_play: :desc )
end
#...
end
app/views/plays/upcoming_plays.html.erb
<!-- This displays links to plays/ID for play occurring in the future-->
<div id="show-all-plays">
<% @plays.each do |current_play| %>
<%= render partial: "layouts/play_link" ,
locals: {play: current_play} %>
<% end %>
</div>
config/routes.rb
Rails.application.routes.draw do
get 'plays/upcoming_plays', :as => 'upcoming_plays', :path=>'upcoming_plays'
#...
end
app/models/ability.rb
class Ability
include CanCan::Ability
def initialize(user)
if user != nil and user.role == 'admin'
can :manage, :all
else
can :read, :all
end
end
end
导航到 /upcoming_plays.html 时收到错误:
You are not authorized to access this page.
它阻止我查看重定向到根页面的页面
我不确定 CanCan 是否相信我正在尝试 manage/modify Plays 模型,但我想做的只是从数据库中读取值。有任何想法吗?感谢您的帮助!
编辑:
Rokibul Hasan 建议添加
skip_authorize_resource :only => :upcoming_plays
到播放控制器。哪个有效!然而,这似乎是一个黑客。如果我想添加管理员在此页面上编辑内容的能力怎么办?我不希望每个人都可以使用禁止的操作。看起来这可能是个问题,有什么想法吗?
看来问题出在你的能力上class,能否请你重写你ability
如下
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.role == 'admin'
can :manage, :all
else
can :read, :all
end
end
end
load_and_authorize_resource
可以自动授权您控制器中的每个操作。对于替代解决方案,您可以将以下代码添加到您的控制器以跳过 upcoming_plays
授权。
skip_authorize_resource :only => :upcoming_plays
我正尝试在自定义路线上提供一些资源,但通过我的 CanCan 访问被拒绝 gem。
具有自定义路由的 PlaysController:upcoming_plays
class PlaysController < ApplicationController
load_and_authorize_resource
def upcoming_plays
@plays = Play.where("date_of_play >= ?",Time.now ).order(date_of_play: :desc )
end
#...
end
app/views/plays/upcoming_plays.html.erb
<!-- This displays links to plays/ID for play occurring in the future-->
<div id="show-all-plays">
<% @plays.each do |current_play| %>
<%= render partial: "layouts/play_link" ,
locals: {play: current_play} %>
<% end %>
</div>
config/routes.rb
Rails.application.routes.draw do
get 'plays/upcoming_plays', :as => 'upcoming_plays', :path=>'upcoming_plays'
#...
end
app/models/ability.rb
class Ability
include CanCan::Ability
def initialize(user)
if user != nil and user.role == 'admin'
can :manage, :all
else
can :read, :all
end
end
end
导航到 /upcoming_plays.html 时收到错误:
You are not authorized to access this page.
它阻止我查看重定向到根页面的页面
我不确定 CanCan 是否相信我正在尝试 manage/modify Plays 模型,但我想做的只是从数据库中读取值。有任何想法吗?感谢您的帮助!
编辑: Rokibul Hasan 建议添加
skip_authorize_resource :only => :upcoming_plays
到播放控制器。哪个有效!然而,这似乎是一个黑客。如果我想添加管理员在此页面上编辑内容的能力怎么办?我不希望每个人都可以使用禁止的操作。看起来这可能是个问题,有什么想法吗?
看来问题出在你的能力上class,能否请你重写你ability
如下
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new
if user.role == 'admin'
can :manage, :all
else
can :read, :all
end
end
end
load_and_authorize_resource
可以自动授权您控制器中的每个操作。对于替代解决方案,您可以将以下代码添加到您的控制器以跳过 upcoming_plays
授权。
skip_authorize_resource :only => :upcoming_plays