In my application_controller.rb
I have this code:
def current_resource
if admin_signed_in?
:admin
elsif partner_signed_in?
:partner
end
end
Now I want to pass this def
to child controllers like this:
authorize_resource current_resource
However it throws me an error. undefined local variable or method
current_resource'`
How I can pass this current_resource
to its child controllers as symbol.
That is how I call it inside controller:
class PageController < ApplicationController
authorize_resource current_resource
end
current_resource
is inside application_controller
This code:
class PageController < ApplicationController
authorize_resource current_resource
end
will be executed during PageController
class loading, not during request handling. So you can't call admin_signed_in?
and partner_signed_in?
then. Also I don't know why are you trying to call authorize_resource with argument, because it doesn't get arguments, check cancancan source.
I think you have misunderstood how cancan filters and abilities work. You shouldn't pass User
model to the filter. User type should be checked in Ability class and based on that, proper permissions should be selected. In your controller (btw. it should be PagesController
) you only load resources and authorize them, ie.:
class PagesController < ApplicationController
load_resource
authorize_resource
# or just load_and_authorize_resource
end
You can customize how resources will be loaded and authorized. Please read this about authorizing controller actions and those examples. And also please read this about defining abilities.