ActionController inherit before_filter

14 views
Skip to first unread message

Peter Hug

unread,
Sep 29, 2014, 4:40:24 PM9/29/14
to rubyonra...@googlegroups.com
I have something like this:



=========================================================================
class ApplicationController < ActionController::Base
def check_auth
# ...
end
end

class BaseController < ApplicationController
before_filter :check_auth, :except => [ :getSession ]
# more here...
def getSession()
# ...
end
end

class UserController < BaseController
before_filter :check_auth, :except => [ :getNotifications ]
# more here...
def getNotifications()
getSession()
# ...
end
end
=========================================================================

I'm was expecting wrongly that if UserController#getNotifications calls
BaseController#getSession check_auth would not called.

If I change the before_filter in my UserController to:

before_filter :check_auth, :except => [ :getSession, :getNotifications
]

it works as expected.

Isn't there a way to incorporate the before_filter settings from the
parent controller say during construction of an instance of a sub class?

Thanks for any help in advance
Pete

--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
Sep 30, 2014, 10:18:35 AM9/30/14
to rubyonra...@googlegroups.com

On Monday, September 29, 2014 9:40:24 PM UTC+1, Ruby-Forum.com User wrote:

class UserController < BaseController
 before_filter :check_auth, :except => [ :getNotifications ]
 # more here...
 def getNotifications()
  getSession()
  # ...
 end
end

 
This strikes me as odd - I wouldn't usually call a parent controller's action explicitly. Either the base controller would implement some non action methods that I'd call or I'd call the base controller functionality via super. 

Having said that, it was my recollection that filters were handled by Action Controller's perform_action so I wouldn't have expected what you saw either

Fred

Peter Hug

unread,
Sep 30, 2014, 5:23:41 PM9/30/14
to rubyonra...@googlegroups.com
Thanks for your feedback Fred.

I should have added that I'm using very old Ruby (1.8.7) and Rails
(2.2.2) versions.

Also, I tried to make my example as simple as possible. In reality, it
is a little more complicated.

The error is that...

...if I invoke an action on the UserController which is implemented in
its base controller, even if the base controller has a before_filter
excluding auth_check to run for that action it will still be run unless
UserController explicitly excludes the auth_check from running for the
inherited action.

I've been debugging this and can see that the debugger first processes
BaseController.before_filter followed by UserController.before_filter
but I don't know how the filters are stored and chained. I think that is
where the problem is.

Frederick Cheung

unread,
Oct 1, 2014, 2:13:42 AM10/1/14
to rubyonra...@googlegroups.com
On Tuesday, September 30, 2014 10:23:41 PM UTC+1, Ruby-Forum.com User wrote:
The error is that...

...if I invoke an action on the UserController which is implemented in
its base controller, even if the base controller has a before_filter
excluding auth_check to run for that action it will still be run unless
UserController explicitly excludes the auth_check from running for the
inherited action.

I've been debugging this and can see that the debugger first processes
BaseController.before_filter followed by UserController.before_filter
but I don't know how the filters are stored and chained. I think that is
where the problem is.


That makes more sense and is now sounding like something i'd expect. You might try  using skip_before_filter .

Fred
Reply all
Reply to author
Forward
0 new messages