Session unrecognized in before_filter block

As the title suggests, Rails doesn’t seem to recognize that a session
is a special hash extended off of ActionController::Base. The error
tells me either session is unrecognized or session is an array that
doesn’t accept symbols as a parameter.

I use before_filter and session as following:
I have several user controllers extending off of each other, like
this: AdminController < ModeraterController, ModeraterController <
MemberController, MemberController < ApplicationController.

In each controller, I either have a before_filter block saying
something like:
before_filter do |controller|
unless session[:member_id]
redirect_to :controller=>‘login’
end
if controller.action_name==‘edit_profile’
unless params[:id]==session[:member_id]
flash[:notice]=“You can’t edit someone else’s profile”
#some redirect command
end
end
end

I also tried the class method (with the self.filter(controller)
method), but with no avail, so I was wondering:

  1. are you allowed to have more than one method of before_filter in
    one controller? For example, is this valid?:
    TempController < ApplicationController
    before_filter :filter1, :only=>‘method1’
    before_filter :filter2, :except=>‘method1’
    #bunch of methods
    end

  2. why isn’t session recognized in the before_filter block method?

  3. I’m guessing the reason why the class method isn’t working is
    because I didn’t extend it off of ApplicationController (though it is
    classes floating in a Controller). If this is so, can I do something
    like?:
    TempController < ApplicationController
    before_filter :self
    #bunch of methods
    self.filter(controller)
    #whatever
    end
    end

Thanks for the help!

I also tried the class method (with the self.filter(controller)
method), but with no avail, so I was wondering:

  1. are you allowed to have more than one method of before_filter in
    one controller? For example, is this valid?:
    TempController < ApplicationController
    before_filter :filter1, :only=>‘method1’
    before_filter :filter2, :except=>‘method1’
    #bunch of methods
    end

Yup.

  1. why isn’t session recognized in the before_filter block method?

It’s using the controller’s class as the local binding since a
controller instance isn’t available at the time of the block’s
creation. That’s why the controller instance is passed. Therefore,
all controller methods need to be called from the controller instance,
not self.

end
Yes, but you don’t need to pass the controller. I find this makes the
filter more self-describing.

before_filter :check_member_access

protected
def check_member_access

end

The nice thing is you also have access to the private/protected
methods of the controller.


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

Huh, I should have tested this out earlier: it would have saved me a
lot of time:).
Thanks for your help!