# How to pass a parameter to a "before\_filter" filter?

**URL:** <https://www.ruby-forum.com/t/how-to-pass-a-parameter-to-a-before-filter-filter/131583>\
**Category:** Rails\
**Created:** [March 11, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-parameter-to-a-before-filter-filter/131583 "2008-03-11T16:42:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fizban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f4b2a3/32.png) [@fizban](https://www.ruby-forum.com/u/fizban)\
**Post date:** [March 11, 2008, 4:42pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-parameter-to-a-before-filter-filter/131583/1 "2008-03-11T16:42:52Z")

</div>

Hi,

I’m very new to rails, so I apologize if my question sounds dumb…

I’m setting up an authentication system for this project. In order to  
login someone has to feed username/password. Since there are several  
portions of the project to be password protected I sticked  
“check\_authentication” under controller/application.rb … I then  
“before\_filter :check\_authentication” from the single controllers in  
need of auth.

But this does not suffice. I need to setup “groups”, because some of  
the controllers should not be accessible to certain groups of users.  
So I added a “role” column to User and I was thinking to add a  
“check\_permission” under application.rb and to add a before\_filter  
from the proper controllers passing the needed role to it somehow

The problem is that I have no idea on how to pass say “administrator”  
to a filter… I tried check\_permission(role) but it doesn’t like it at  
all

Any hint? 🙂

Thanks

---

<div class="post-metadata">

**Author:** ![fizban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f4b2a3/32.png) [@fizban](https://www.ruby-forum.com/u/fizban)\
**Post date:** [March 11, 2008, 4:48pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-parameter-to-a-before-filter-filter/131583/2 "2008-03-11T16:48:10Z")

</div>

On 11 Mar 2008, at 15:41, fizban wrote:

> The problem is that I have no idea on how to pass say “administrator”  
> to a filter… I tried check\_permission(role) but it doesn’t like it at  
> all  
> In short, you can’t. But you can do this:  
> Stick  
> def self.require\_role role, options = {}  
> define\_method role +’\_required’ do  
> check\_permission role  
> end  
> protected role+’\_required’  
> before\_filter (role+’\_required’).to\_sym, options  
> end  
> in application.rb (or do whatever you want to have that as a class  
> method)

You can then write  
require\_role ‘admin’  
in a controller for example and that will create an admin\_required  
method (that just calls through to check\_permission) and set it up as  
a before\_filter.

Fred

---

<div class="post-metadata">

**Author:** ![fizban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f4b2a3/32.png) [@fizban](https://www.ruby-forum.com/u/fizban)\
**Post date:** [March 11, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-parameter-to-a-before-filter-filter/131583/3 "2008-03-11T17:03:48Z")

</div>

On 11 Mar, 16:47, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> In short, you can’t. But you can do this:  
> [snip]

Thank you, it works just fine 🙂

---

<div class="post-metadata">

**Author:** ![fizban](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f4b2a3/32.png) [@fizban](https://www.ruby-forum.com/u/fizban)\
**Post date:** [March 11, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/how-to-pass-a-parameter-to-a-before-filter-filter/131583/4 "2008-03-11T17:23:52Z")

</div>

Well, I panic’d for a couple of minutes… so, if anyone’s reading this  
and is willing to use Fred’s method, keep in mind to stick  
“require\_role ‘blah’” AFTER the before\_filter line or you’ll get a nil  
related error if the user is not currently logged in
