# Automatic 'verify :xhr =\> true' for methods ending with \_xhr

**URL:** <https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010>\
**Category:** Rails\
**Created:** [December 3, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010 "2007-12-03T20:42:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 3, 2007, 8:42pm UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010/1 "2007-12-03T20:42:28Z")

</div>

hey guys,  
i am writing a couple of actions which are only used with an xhr. I  
‘protect’ them all against a direct access with the ‘verify’ method in  
the controller

verify :only =\> …, :xhr =\> true

it would be cool if the system would automatically recognize the xhr  
actions and protect them … i want to postfix methods with ‘\_xhr’ and  
those should be only accessible with an xhr.

- good for readability
- good for my DRY

does anyone has a tip on how to implement this? i guess it should go  
into the ‘ApplicationController’

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 3, 2007, 8:55pm UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010/2 "2007-12-03T20:55:27Z")

</div>

A before\_filter is all you really need as that’s kinda what verify is  
anyway

- a DSL for creating before\_filters.

before\_filter :protect\_xhr\_methods

def protect\_xhr\_methods  
redirect\_to :action=\>“index” unless self.action\_name.include?("\_xhr")  
&&  
request.xhr?  
end

(I did not test this, and there’s a better way to test for \_xhr in the  
action name, but I am really lazy right now 🙂 )

Is that something close?

On Dec 3, 2007 1:42 PM, Michal G. \<

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 3, 2007, 9:20pm UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010/3 "2007-12-03T21:20:19Z")

</div>

yeah that was exactly what i needed… thanks…  
the only problem i have now is that i have two before\_filters:  
before\_filter :protect\_xhr, check\_login

and both can do a redirect … when protect\_xhr does one already and  
check\_login also wants to do one i get a double response error …

whats the best solution for this?

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 4, 2007, 5:06am UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010/4 "2007-12-04T05:06:38Z")

</div>

In a filter, make sure to return false and not just to render /  
redirect. You should always return false in a filter if you want to halt  
the execution chain.

i.e redirect\_to my\_url and false

Michal G. wrote:

> yeah that was exactly what i needed… thanks…  
> the only problem i have now is that i have two before\_filters:  
> before\_filter :protect\_xhr, check\_login
> 
> and both can do a redirect … when protect\_xhr does one already and  
> check\_login also wants to do one i get a double response error …
> 
> whats the best solution for this?

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 4, 2007, 11:34am UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010/5 "2007-12-04T11:34:13Z")

</div>

Brian H. wrote:

> @Nathan:
> 
> While this is _currently_ true, things have changed in Rails 2.0, and  
> that’s  
> where I’ve been working a lot lately. Thank you for reminding of this.
> 
> In Rails 2.0 you’ll no longer need to worry about that, as redirecting  
> or  
> explicitly rendering will stop the filter execution. (Yay!)
> 
> [http://dev.rubyonrails.org/changeset/7984](http://dev.rubyonrails.org/changeset/7984)

hey thats great!! thanks for your participation

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 4, 2007, 6:06am UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010/6 "2007-12-04T06:06:56Z")

</div>

@Nathan:

While this is _currently_ true, things have changed in Rails 2.0, and  
that’s  
where I’ve been working a lot lately. Thank you for reminding of this.

In Rails 2.0 you’ll no longer need to worry about that, as redirecting  
or  
explicitly rendering will stop the filter execution. (Yay!)

[http://dev.rubyonrails.org/changeset/7984](http://dev.rubyonrails.org/changeset/7984)

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 4, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010/7 "2007-12-04T22:41:46Z")

</div>

On Dec 3, 2007 11:06 PM, Brian H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> While this is _currently_ true, things have changed in Rails 2.0, and that’s  
> where I’ve been working a lot lately. Thank you for reminding of this.
> 
> In Rails 2.0 you’ll no longer need to worry about that, as redirecting or  
> explicitly rendering will stop the filter execution. (Yay!)
> 
> [http://dev.rubyonrails.org/changeset/7984](http://dev.rubyonrails.org/changeset/7984)

/me watches his Rails books depreciate even faster than last time

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 4, 2007, 12:10pm UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010/8 "2007-12-04T12:10:32Z")

</div>

Yea, I am aware that they changed that and I am really glad they did. I  
always thought that the whole “returning false” thing was counter  
intuitive. Everyone coming in expects a render or a redirect to stop the  
execution. I always felt that was the better solution. Glad the rails  
core finally agreed.

Brian H. wrote:

> @Nathan:
> 
> While this is _currently_ true, things have changed in Rails 2.0, and  
> that’s  
> where I’ve been working a lot lately. Thank you for reminding of this.
> 
> In Rails 2.0 you’ll no longer need to worry about that, as redirecting  
> or  
> explicitly rendering will stop the filter execution. (Yay!)
> 
> [http://dev.rubyonrails.org/changeset/7984](http://dev.rubyonrails.org/changeset/7984)

---

<div class="post-metadata">

**Author:** ![gabru](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/c0e974/32.png) [@gabru](https://www.ruby-forum.com/u/gabru)\
**Post date:** [December 7, 2007, 9:06pm UTC](https://www.ruby-forum.com/t/automatic-verify-xhr-true-for-methods-ending-with-xhr/122010/9 "2007-12-07T21:06:08Z")

</div>

Greg:

Just be glad you aren’t trying to follow the Agile book NOW, when  
Rails 2.0is installed when you do a fresh installation 🙂 I feel  
really sorry for any  
newbies right now.
