# Session unrecognized in before\_filter block

**URL:** <https://www.ruby-forum.com/t/session-unrecognized-in-before-filter-block/110852>\
**Category:** Rails\
**Created:** [July 31, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/session-unrecognized-in-before-filter-block/110852 "2007-07-31T15:44:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [July 31, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/session-unrecognized-in-before-filter-block/110852/1 "2007-07-31T15:44:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [July 31, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/session-unrecognized-in-before-filter-block/110852/2 "2007-07-31T15:48:41Z")

</div>

> 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://lighthouseapp.com)  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![taro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/df705f/32.png) [@taro](https://www.ruby-forum.com/u/taro)\
**Post date:** [July 31, 2007, 3:50pm UTC](https://www.ruby-forum.com/t/session-unrecognized-in-before-filter-block/110852/3 "2007-07-31T15:50:42Z")

</div>

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