# Filter function with parameter

**URL:** <https://www.ruby-forum.com/t/filter-function-with-parameter/62409>\
**Category:** Rails\
**Created:** [June 4, 2006, 7:42am UTC](https://www.ruby-forum.com/t/filter-function-with-parameter/62409 "2006-06-04T07:42:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mh789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mh789](https://www.ruby-forum.com/u/mh789)\
**Post date:** [June 4, 2006, 7:42am UTC](https://www.ruby-forum.com/t/filter-function-with-parameter/62409/1 "2006-06-04T07:42:48Z")

</div>

Hi!

I want to add a function with a static paramter (“2” in the example) to  
a filter, but somehow Rails seems to be looking for another syntax.

before\_filter :check\_quantity(2), :only =\> [:show]

doesn’t work. What’s the right way to do this?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![mh789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mh789](https://www.ruby-forum.com/u/mh789)\
**Post date:** [June 4, 2006, 11:41am UTC](https://www.ruby-forum.com/t/filter-function-with-parameter/62409/2 "2006-06-04T11:41:02Z")

</div>

If the parameter is static, then won’t the result be static too? If  
so, you could just put in the result.  
-Nathan

---

<div class="post-metadata">

**Author:** ![mh789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mh789](https://www.ruby-forum.com/u/mh789)\
**Post date:** [June 4, 2006, 12:35pm UTC](https://www.ruby-forum.com/t/filter-function-with-parameter/62409/3 "2006-06-04T12:35:41Z")

</div>

> If the parameter is static, then won’t the result be static too? If  
> so, you could just put in the result.

No, as a filter has access to other variables as well.

Another example would be:

before\_filter :check\_authentication(:user), :only =\> [:list, :show]  
before\_filter :check\_authentication(:admin), :only =\> [:create, :delete]  
(Sure, I could go with two separate functions  
(check\_admin\_authentication and check\_user\_authentication) in this  
particular example.)

I’m sure this feature is commonly needed, so it should be implemented in  
Rails. I just don’t know how to use it.

---

<div class="post-metadata">

**Author:** ![mh789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mh789](https://www.ruby-forum.com/u/mh789)\
**Post date:** [June 5, 2006, 6:32pm UTC](https://www.ruby-forum.com/t/filter-function-with-parameter/62409/4 "2006-06-05T18:32:04Z")

</div>

On Jun 4, 2006, at 3:35 AM, mh789 wrote:

> (Sure, I could go with two separate functions  
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

```
You could just give your method a default parameter, and then when

```

it gets called without a param it will always be 2 but when you call  
it form elsewhere you can give it whatever arg you want to.

before\_filter :check\_quantity, :only =\> [:show]

def check\_quantity(quan = 2)

# unless you call with an arg, quan will always be 2.

end

-Ezra

---

<div class="post-metadata">

**Author:** ![mh789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mh789](https://www.ruby-forum.com/u/mh789)\
**Post date:** [June 6, 2006, 2:38am UTC](https://www.ruby-forum.com/t/filter-function-with-parameter/62409/5 "2006-06-06T02:38:24Z")

</div>

You can use blocks in a before\_filter. I’m not at home at the moment  
so I have no ruby to check syntax, but something like

before\_filter :only =\> [:show], { check\_authentication(:user) }

Sorry if the syntax is not correct. I have used this kind of filter  
before to pass parameters to methods. Note that if you need a filter  
to make the user avaialbe then you need to call that filter before  
this one.

eg  
before\_filter :set\_user  
before\_filter :only =\> [:show], { check\_authentication(:user) }

Hope this helps

---

<div class="post-metadata">

**Author:** ![mh789](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/91b2a8/32.png) [@mh789](https://www.ruby-forum.com/u/mh789)\
**Post date:** [June 7, 2006, 10:06am UTC](https://www.ruby-forum.com/t/filter-function-with-parameter/62409/6 "2006-06-07T10:06:15Z")

</div>

Daniel ----- wrote:

> You can use blocks in a before\_filter. I’m not at home at the moment  
> so I have no ruby to check syntax, but something like
> 
> before\_filter :only =\> [:show], { check\_authentication(:user) }

Thanks a lot, that does the trick!

before\_filter(:only =\> [:create, :delete]) {|controller|  
controller.check\_authentication(‘admin’) }
