before_filter and xhr

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

<div id="current_poll"> <%= render :partial => 'polls/poll' %> </div>

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

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?

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