# Before\_filter symbol with params

**URL:** <https://www.ruby-forum.com/t/before-filter-symbol-with-params/105828>\
**Category:** Rails\
**Created:** [June 3, 2007, 1:57pm UTC](https://www.ruby-forum.com/t/before-filter-symbol-with-params/105828 "2007-06-03T13:57:54Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaper](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/958977/32.png) [@shaper](https://www.ruby-forum.com/u/shaper)\
**Post date:** [June 3, 2007, 1:57pm UTC](https://www.ruby-forum.com/t/before-filter-symbol-with-params/105828/1 "2007-06-03T13:57:54Z")

</div>

I had a need to do something like:

before\_filter :require\_permissions, :slot =\> :forums, :only =\> :edit

where I wanted require\_permissions to be able to access the non-filter-  
config-related hash params. However, unlike custom model validators,  
on spelunking into the rails controller filter code it appears the  
config hash params aren’t available to the filter method when declared  
this way – not even the non-filter-specific (e.g. :slot) params.

I did something like what dblack has suggested elsewhere on this forum  
ala:

before\_filter Proc.new { |controller|  
controller.send(:require\_permissions, :forums) }, :only =\> :edit

which should be analogous to:

before\_filter :only =\> :edit { |controller|  
controller.send(:require\_permissions, :forums) }

Things that bum me out about this:

- require\_permissions is protected (which it should be, so it’s not a  
public action that could incorrectly be called by a web request) so  
you can’t just do controller.require\_permissions(:forums) in the  
block, so I had to directly monkey the method-hiding by calling  
controller.send.

- what could be nice and concise (the first version of what I wanted)  
becomes much longer and more verbose.

This would seem like a common pattern lots of folks might want, to  
make it easy to use a symbol when specifying a filter while still  
specifying some options, just like the model validators where you can  
do, for instance (lifted from some [dotsrc.org](http://dotsrc.org) code):

def self.validates\_url(\*attr\_names)  
conf = { :message =\> “is not a well-formed URL”,  
:on =\> :save,  
:allow\_nil =\> false,  
:schemes =\> [‘http’, ‘https’, ‘ftp’] }  
conf.update(attr\_names.pop) if attr\_names.last.is\_a?(Hash)  
[…]  
end

where you see custom params such as :schemes that they can do with  
what they will.

Is there a better way?

–  
Walter Korman – [http://lemurware.blogspot.com](http://lemurware.blogspot.com)
