0

I am building application that currently has a simple authorization requirments:

All users can view the data of all controllers. Only Admin can create/edit the data. The project use admin gem so no controllers in my application (although I can overide if needed) I would like to add authorization for the generic requirement for all controllers:

class ApplicationController < Administrate::ApplicationController
  before_filter :authenticate_admin
  def authenticate_admin
    authorize requested_resource # requested_form is the relevant object(e.g. Post @post)
  end
end

but then I get the following:

unable to find scope NilClassPolicy::Scope for NilClass app/policies/application_policy.rb:41:in scope' app/policies/application_policy.rb:15:inshow?' app/controllers/admin/application_controller.rb:27:in `authenticate_admin'

Seems like it can't find the policy, what do you think?

user664859
  • 153
  • 2
  • 13

1 Answers1

0

I'm not sure you still need help with this particular question, but since I found it while doing research for a very similar issue I figured I would still put my two cents in, because I was able to figure out the reason I was receiving a similar exception.

I had mistakenly called authorize in my controller i.e.:

# customer_contoller.rb  
def show 
  authorize @customer
end

However, I had not created a corresponding def show? method in customer_policy.rb. All I had to do was create the corresponding show? method and it worked!

So I would imagine that this exception is caused when there is a mismatch between calling authorize in the controller and the policies that you have setup. Specifically when you haven't created a policy for the action you are asking to authorize.

Hope that helps someone at least get moving in the right direction.