2013-07-04 20 views
0

我在Rails(3.2.13)上使用Ruby(1.9),并使用devise和cancan进行身份验证和授权。我在控制器中进行了自定义操作,它在开发环境中正确地与匿名用户一起使用。当我将它部署在Heroku中时,同一页面会抛出“未授权”错误。我在本地设置了RAILS_ENV =生产,并且出现了相同的错误。但是当我将RAILS_ENV重新设置为开发时,它开始正常工作。Rails 3.2 - 在CanCan中自定义动作的拒绝访问异常 - 但仅在生产中

我看不到任何特定于CanCan的环境配置。从文档和示例来看,它看起来并不像我需要更多东西来完成这项工作。有人可以帮忙吗?下面的代码:

的config/routes.rb中

... 
resources :venues do 
    ... 
    ... 
    collection do 
     get 'testaction' 
    end 
... 

型号/ ability.rb

class Ability 
    include CanCan::Ability 
    def initialize(user) 
    user ||= User.new # guest user 
    if user.role? :admin 
     can :manage, :all 
    else 
     can :testaction, Venue 
    end 
    end 
end 

控制器/ venues_controller.rb

class VenuesController < ApplicationController 
    before_filter :authenticate_user!, :except => [:testaction] #tried with & w/o this line 
    load_and_authorize_resource 

    def testaction 
     respond_to do |format| 
      format.html 
     end 
    end 
    ... 
    ... 
end 

我也尝试在路由中添加members而不是collections下的自定义操作,但行为是相同的。

谢谢!

回答

2

我也收到类似的用户错误,然后我发现了一个很好的工作存储库,用于了解用于授权和验证角色的cancan流程。

请按照存储库提供的说明进行操作,并按步骤匹配您的代码步骤。

https://github.com/RailsApps/rails3-bootstrap-devise-cancan

我建议在你的控制器使用authorize_resource :class => falseenter code here代替load_and_authorize_resource。我认为它会帮助你。

谢谢。

+1

谢谢!我看了一下这个项目,并按照你的建议改变了它。下面也工作,所以我不必把授权!在每个方法中.. '类VenuesController [:testaction] \t load_and_authorize_resource:除了=> [:testaction] ' – Narayana

相关问题