# Passing Arguments to before\_filter

**URL:** <https://www.ruby-forum.com/t/passing-arguments-to-before-filter/89515>\
**Category:** Rails\
**Created:** [March 20, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/passing-arguments-to-before-filter/89515 "2007-03-20T23:33:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [March 20, 2007, 11:33pm UTC](https://www.ruby-forum.com/t/passing-arguments-to-before-filter/89515/1 "2007-03-20T23:33:12Z")

</div>

I want to put a before\_filter in application.rb, as follows:

before\_filter :do\_something, :except\_controller =\> [:admin]

def do\_something(options)  
options.stringify\_keys!  
unless options[:except\_controller].include?(controller\_name)  
# do something useful  
end  
end

Where I’m blocking is on how to get the ‘options’ data in the  
do\_something  
method. I think I can write this as:

## Thanks,

View this message in context:  
[http://www.nabble.com/Passing-Arguments-to-before\_filter-tf3436997.html#a9583142](http://www.nabble.com/Passing-Arguments-to-before_filter-tf3436997.html#a9583142)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![cwd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/da6949/32.png) [@cwd](https://www.ruby-forum.com/u/cwd)\
**Post date:** [March 21, 2007, 12:36am UTC](https://www.ruby-forum.com/t/passing-arguments-to-before-filter/89515/2 "2007-03-21T00:36:04Z")

</div>

Hi –

On 3/20/07, s.ross [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> end
> 
> Where I’m blocking is on how to get the ‘options’ data in the do\_something  
> method. I think I can write this as:

The way I’ve done this in the past, which may or may not be the  
slickest way to do it, is along the lines of:

class MyController \< ApplicationController  
before\_filter :only =\> “whatever” do |controller|  
controller.some\_method(1,2,3)  
end

```
def some_method(a,b,c)
   ...
end

```

end

The proc has the controller object itself yielded to it, so you can  
then call the method on that object with whatever arguments you need.

The particular case you’ve got, where you seem to be testing for  
whether this controller needs a particular filter, looks like it would  
probably better handled with skip\_before\_filter and friends. But  
anyway, you can grab the controller object and call methods on it  
directly.

David

–  
Q. What is THE Ruby book for Rails developers?  
A. RUBY FOR RAILS by David A. Black ([Ruby for Rails - David A. Black | Manning](http://www.manning.com/black))  
(See what readers are saying! [http://www.rubypal.com/r4rrevs.pdf](http://www.rubypal.com/r4rrevs.pdf))  
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?  
A. Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
