# Before\_filter at the method level

**URL:** <https://www.ruby-forum.com/t/before-filter-at-the-method-level/118218>\
**Category:** Rails\
**Created:** [October 24, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/before-filter-at-the-method-level/118218 "2007-10-24T22:00:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimmypalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jimmypalmer](https://www.ruby-forum.com/u/jimmypalmer)\
**Post date:** [October 24, 2007, 10:00pm UTC](https://www.ruby-forum.com/t/before-filter-at-the-method-level/118218/1 "2007-10-24T22:00:41Z")

</div>

I have used before\_filter in the passed at the class level but can it be  
used in the method level.

For example, I want to refuse a non-admin type from being able to delete  
anything in the entire application. Is there an easy way to do this?

---

<div class="post-metadata">

**Author:** ![jimmypalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jimmypalmer](https://www.ruby-forum.com/u/jimmypalmer)\
**Post date:** [October 24, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/before-filter-at-the-method-level/118218/2 "2007-10-24T22:14:58Z")

</div>

On 10/24/07, Jimmy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have used before\_filter in the passed at the class level but can it be  
> used in the method level.

You can use :only to apply the filter to specific actions in your  
controller:

before\_filter :require\_admin\_login, :only =\> [ :delete, :blah, :blech  
]

is that what you’re after?

---

<div class="post-metadata">

**Author:** ![jimmypalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jimmypalmer](https://www.ruby-forum.com/u/jimmypalmer)\
**Post date:** [October 24, 2007, 10:22pm UTC](https://www.ruby-forum.com/t/before-filter-at-the-method-level/118218/3 "2007-10-24T22:22:41Z")

</div>

Bob S. wrote:

> On 10/24/07, Jimmy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have used before\_filter in the passed at the class level but can it be  
> > used in the method level.
> 
> You can use :only to apply the filter to specific actions in your  
> controller:
> 
> before\_filter :require\_admin\_login, :only =\> [ :delete, :blah, :blech  
> ]
> 
> is that what you’re after?

Yes! Thank you.

Now, is there a way to handle the denied request? So, if a non-admin  
user trys to go to delete is there a way to universal respond with “you  
don’t have access”?

---

<div class="post-metadata">

**Author:** ![jimmypalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jimmypalmer](https://www.ruby-forum.com/u/jimmypalmer)\
**Post date:** [October 24, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/before-filter-at-the-method-level/118218/4 "2007-10-24T22:38:29Z")

</div>

Bob S. wrote:

> On 10/24/07, Jimmy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Now, is there a way to handle the denied request? So, if a non-admin  
> > user trys to go to delete is there a way to universal respond with “you  
> > don’t have access”?
> 
> You can redirect or render from your filter (you should also return  
> false, but I don’t think that’s strictly required anymore if you  
> render or redirect).

AH! of course. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![jimmypalmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jimmypalmer](https://www.ruby-forum.com/u/jimmypalmer)\
**Post date:** [October 24, 2007, 10:32pm UTC](https://www.ruby-forum.com/t/before-filter-at-the-method-level/118218/5 "2007-10-24T22:32:31Z")

</div>

On 10/24/07, Jimmy P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Now, is there a way to handle the denied request? So, if a non-admin  
> user trys to go to delete is there a way to universal respond with “you  
> don’t have access”?

You can redirect or render from your filter (you should also return  
false, but I don’t think that’s strictly required anymore if you  
render or redirect).
