# Before filters with parameters AND conditions

**URL:** <https://www.ruby-forum.com/t/before-filters-with-parameters-and-conditions/146007>\
**Category:** Rails\
**Created:** [August 16, 2008, 1:41am UTC](https://www.ruby-forum.com/t/before-filters-with-parameters-and-conditions/146007 "2008-08-16T01:41:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![totti83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@totti83](https://www.ruby-forum.com/u/totti83)\
**Post date:** [August 16, 2008, 1:41am UTC](https://www.ruby-forum.com/t/before-filters-with-parameters-and-conditions/146007/1 "2008-08-16T01:41:32Z")

</div>

Hi, I want to use a method with parameters in the before\_filter  
callback, but also use some conditions like :only =\> :edit.

For example:

before\_filter { |c| c.auth ‘admin’, :only =\> :edit }

Off course this is not working, I want to know if there is a way of do  
this (I googled a lot, obviously without success).

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![totti83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@totti83](https://www.ruby-forum.com/u/totti83)\
**Post date:** [August 16, 2008, 3:02am UTC](https://www.ruby-forum.com/t/before-filters-with-parameters-and-conditions/146007/2 "2008-08-16T03:02:59Z")

</div>

Franco C. wrote:

> Hi, I want to use a method with parameters in the before\_filter  
> callback, but also use some conditions like :only =\> :edit.
> 
> For example:
> 
> before\_filter { |c| c.auth ‘admin’, :only =\> :edit }
> 
> Off course this is not working, I want to know if there is a way of do  
> this (I googled a lot, obviously without success).
> 
> Thanks in advance.

If I understand what you’re trying to do (and I very well may not be),  
you are wanting to limit access to an action to an administrative user.  
If that assumption is correct, I think most people do that by having a  
concept of “current\_user” and then having the before\_filter check with  
current\_user. Something like

before\_filter :require\_admin, :only =\> :edit

def require\_admin  
current\_user.admin?  
end

Then you can do whatever you want in current\_user.admin? to determine if  
the user is, in fact, an administrator.

If I’ve misunderstood, sorry.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![totti83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@totti83](https://www.ruby-forum.com/u/totti83)\
**Post date:** [August 16, 2008, 3:25am UTC](https://www.ruby-forum.com/t/before-filters-with-parameters-and-conditions/146007/3 "2008-08-16T03:25:04Z")

</div>

Thanks for your answer, you understand correctly. The point is, I want  
to use the same function for autenticate admins and bare users, for  
example:

def auth(role = :user)  
if @user.role != role  
redirect\_to :controller =\> :users, :action =\> :login  
end  
end

So in some cases I need to use the auth(:admin) and some times the  
auth(:user) even in the same controller and with exceptions like the  
action ‘login’ that don’t need authentication.

PD: Sorry for my English… =)

On 15 ago, 22:02, Phillip K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![totti83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@totti83](https://www.ruby-forum.com/u/totti83)\
**Post date:** [August 16, 2008, 10:22pm UTC](https://www.ruby-forum.com/t/before-filters-with-parameters-and-conditions/146007/4 "2008-08-16T22:22:44Z")

</div>

This work perfect, thanks for the answers.

---

<div class="post-metadata">

**Author:** ![totti83](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@totti83](https://www.ruby-forum.com/u/totti83)\
**Post date:** [August 16, 2008, 3:36am UTC](https://www.ruby-forum.com/t/before-filters-with-parameters-and-conditions/146007/5 "2008-08-16T03:36:47Z")

</div>

On 16 Aug 2008, at 02:24, Franco C. wrote:

> So in some cases I need to use the auth(:admin) and some times the  
> auth(:user) even in the same controller and with exceptions like the  
> action ‘login’ that don’t need authentication.

how about

def self.auth(role, \*args)  
define\_method “auth\_#{role}\_filter” do  
if @user.role != role

> ```
> redirect_to :controller => :users, :action => :login
> 
> ```
> 
> end  
> end  
> before\_filter “auth\_#{role}\_filter”.to\_sym, \*args  
> end

Now in your controller you can say

auth(:user, :only =\> :some\_action)

you can pass any option you would normally pass to before\_filter, eg

auth(:user, :except =\> [:some\_other\_action, :something\_else])

Fred
