# Sending additional parameters to before\_filter

**URL:** <https://www.ruby-forum.com/t/sending-additional-parameters-to-before-filter/98423>\
**Category:** Rails\
**Created:** [July 14, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/sending-additional-parameters-to-before-filter/98423 "2006-07-14T17:47:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [July 14, 2006, 5:47pm UTC](https://www.ruby-forum.com/t/sending-additional-parameters-to-before-filter/98423/1 "2006-07-14T17:47:07Z")

</div>

i am trying to create a system so that different users have different  
priviliges on my application. i was going to modify my authorize  
function so that i could pass the required role to it and have it check  
the current user for that role.

i am using before\_filter and would like to have just one function  
instead of writing one for each role like so:

authorize(role)

instead of:

authorize\_customer  
and  
authorise\_csr

but i don’t know what i need to do so that i can do something like:

before\_filter :authorize(‘Admin’)

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [July 14, 2006, 5:54pm UTC](https://www.ruby-forum.com/t/sending-additional-parameters-to-before-filter/98423/2 "2006-07-14T17:54:59Z")

</div>

one more thing i was thinking about too was using dynamic function names  
like rails does ~~\> User.find\_by\_username(’\*\*\*\*\*’)

if i could have a before\_filter :authorize\_user and in another  
controller have before\_filter :authorize\_admin and have them go to the  
same function, that would work too, but i don’t know how to do that in  
ruby yet.

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [July 14, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/sending-additional-parameters-to-before-filter/98423/3 "2006-07-14T17:56:52Z")

</div>

Josh K. wrote:

> instead of:
> 
> authorize\_customer  
> and  
> authorise\_csr
> 
> but i don’t know what i need to do so that i can do something like:
> 
> before\_filter :authorize(‘Admin’)

Send in a thunk, of course:

before\_filter { authorize(‘Admin’) }

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [July 14, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/sending-additional-parameters-to-before-filter/98423/4 "2006-07-14T18:01:19Z")

</div>

Josh K. wrote:

> one more thing i was thinking about too was using dynamic function names  
> like rails does ~~\> User.find\_by\_username(‘\*\*\*\*\*’)
> 
> if i could have a before\_filter :authorize\_user and in another  
> controller have before\_filter :authorize\_admin and have them go to the  
> same function, that would work too, but i don’t know how to do that in  
> ruby yet.

One way to do it would be to add the filter to the base controller  
instead, and use the solution I wrote in the last mail.

–  
Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
JvYAML, RbYAML, JRuby and Jatha contributor  
System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [July 14, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/sending-additional-parameters-to-before-filter/98423/5 "2006-07-14T18:40:32Z")

</div>

thanks for the help. i’m still having some problems though. when i try  
the code that you gave, it only works when the method is in the current  
controller. when i try it and my method is in application.rb, it says  
that the method isn’t found.

Ola B. wrote:

> Josh K. wrote:
> 
> > instead of:
> > 
> > authorize\_customer  
> > and  
> > authorise\_csr
> > 
> > but i don’t know what i need to do so that i can do something like:
> > 
> > before\_filter :authorize(‘Admin’)
> 
> Send in a thunk, of course:
> 
> before\_filter { authorize(‘Admin’) }
> 
> –  
> Ola B. ([http://ola-bini.blogspot.com](http://ola-bini.blogspot.com))  
> JvYAML, RbYAML, JRuby and Jatha contributor  
> System Developer, Karolinska Institutet ([http://www.ki.se](http://www.ki.se))  
> OLogix Consulting ([http://www.ologix.com](http://www.ologix.com))
> 
> “Yields falsehood when quined” yields falsehood when quined.
