# Before\_filter and xhr

**URL:** <https://www.ruby-forum.com/t/before-filter-and-xhr/123259>\
**Category:** Rails\
**Created:** [December 15, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/before-filter-and-xhr/123259 "2007-12-15T16:09:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [December 15, 2007, 4:09pm UTC](https://www.ruby-forum.com/t/before-filter-and-xhr/123259/1 "2007-12-15T16:09:17Z")

</div>

I’m making some ajax calls via prototype’s new Ajax.Request method.  
When I do that, are before\_filters fired? This is Rails 1.2.3, and  
here is what I’m doing:

in a controller, I have

before\_fllter :get\_poll

which assigns global variables by querying the database for the  
current poll (if there is one) and checking to see if the current  
user has voted in it yet (must be logged in to vote).

in a view, I have

\<%= render :partial =\> 'polls/poll' %\>

Using the globals defined in the get\_poll before filter, there is  
logic In the \_polls partial to display the voting form if the user  
hasn’t voted yet or the current results if the user has.

The Vote button on the poll form calls a javascript function that  
ultimately does this

new Ajax.Request(’/wgg/vote’, {  
method: ‘post’,  
parameters: $(‘poll\_form’).serialize(),  
onFailure: function() { alert(‘Unable to cast your vote.’); },  
onException: function(o, e) { alert(e); }  
});

and the controller action ‘vote’ ends with

render :update do |page|  
page.replace\_html ‘current\_poll’, :partial =\> ‘polls/poll’  
end

When I vote, the vote is recorded in the database, but the vote form  
is rendered again, not the results. If I explicitly call get\_poll  
before the render :update, like this

get\_poll  
render :update do |page|  
page.replace\_html ‘current\_poll’, :partial =\> ‘polls/poll’  
end

it works correctly. It seems like the before filter get\_poll is not  
getting called for the xhr request. Is that the case?

Hmm. Now that I think about it, before filters are getting called.  
They have to be. The very first before\_filter checks to see if  
someone is logged in, and since the vote gets recorded against my  
login, it seems that the before filter was executed properly. Here  
is the list of before filters in this controller

before\_filter :get\_person  
before\_filter :require\_no\_login, :only =\>  
[:sign\_up, :login, :forgot\_password]  
before\_filter :set\_includes, :except =\>  
[:directory, :submit\_task, :request\_group, :vote]  
before\_filter :require\_login, :only =\>  
[:vote, :submit\_task, :request\_group, :directory, :directory\_message]  
before\_filter :get\_poll  
before\_filter :set\_flash\_variables

You can see that require\_no\_login and set\_includes won’t get called  
when voting, so only get\_person and require\_login could stop get\_poll  
from being called. But since the vote does actually get recorded,  
the vote action is invoked, so no before\_filter ended the chain.  
Besides, there’s nothing in the log about the filter chain ending.

I’m confused. Enlightenment appreciated.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![pkoebbe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ec9cab/32.png) [@pkoebbe](https://www.ruby-forum.com/u/pkoebbe)\
**Post date:** [December 15, 2007, 5:03pm UTC](https://www.ruby-forum.com/t/before-filter-and-xhr/123259/2 "2007-12-15T17:03:10Z")

</div>

On Dec 15, 2007, at 9:08 AM, Phillip K. wrote:

> it works correctly. It seems like the before filter get\_poll is  
> not getting called for the xhr request. Is that the case?

snip

> I’m confused. Enlightenment appreciated.

I’ll say I am. I had a “Doh!” moment when I realized it’s a _before_  
filter. Of course it’s going to show me the vote form again because  
the before filter runs _before_ my vote is committed to the  
database. Sigh.

Peace,  
Phillip
