mardi 23 février 2016

CanCan::AccessDenied: You are not authorized to access this page. Rails

While performing Unit Test using Rake on my Rails4 application, where I have used CanCan, It is giving me an access denied error while testing the index. The application is running fine, dont know how to test the index. Kindly, help me out in this one. Thanks in advance.

FittingsurveysControllerTest Error :-

FittingsurveysControllerTest#test_should_get_index:
CanCan::AccessDenied: You are not authorized to access this page.
test/controllers/fittingsurveys_controller_test.rb:15:in `block in <class:FittingsurveysControllerTest>'

fittingsurveys_controller_test.rb

test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:fittingsurveys)
end

Application Controller -

 def access_denied(exception)
    redirect_to new_user_session_path, :alert => exception.message
end

Config/initializers/rails_admin.rb

RailsAdmin.config do |config 
config.authorize_with :cancan
config.current_user_method &:current_user

Fittingsurveys_controller.rb

class FittingsurveysController < ApplicationController
before_action :set_fittingsurvey, only: [:show, :edit, :update, :destroy]
before_filter :capture_referrer
load_and_authorize_resource
respond_to :html
def index
@fittingsurveys = Fittingsurvey.all
respond_with(@fittingsurveys)
end

app/models/ability.rb

class Ability
include CanCan::Ability
def initialize(user)
def initialize(user)
can :read, :all 
can [:new,:create,:show,:update], [Fittingsurvey,UserFeedback]
cannot :index,[Fittingsurvey,User]

if user && user.role.name=="ADMIN"
can :manage, :all
can :access, :rails_admin
can :export, :all
can :manage, :all
can :manage, User
can :index, [Fittingsurvey,User,UserFeedback]
cannot :index,[Address,Fittingblackout]
end
end
end

Aucun commentaire:

Enregistrer un commentaire