Rich Collins successfully answered ParagramStudios's question:

I'm building a RoR application that uses sever Ajax.Updater's that load partials into DIVs on the page. Unfortunately is the user's session expires it loads the contents of '/user/login' into the DIV that was updated by the Ajax.Updater. Furthermore, this login for doesn't work like the one at '/user/login' **What is the best way around this problem?** A valid answer would include some sample code or a link to some. Ideally the solution would happen on the Rails side of things, but if there is an elegant solution in the form of modify the prototype.js I would accept that as well.

People succeed in answering ParagramStudios's questions 26% of the time (9 successes in 34 attempts).

Answers by: Rich Collins

Rich Collins's Answer:

Reply by Rich Collins 840 days ago

This is actually not that bad. You need to modify your redirect to login code. When the request is a XMLHttpRequest, you return javascript that will change the location to the login page. When it is not, then you use a normal redirect:

before_filter :redirect_unless_authenticated

def redirect_unless_authenticated
unless authenticated?
if request.xhr?
render :update do |page|
page.redirect_to :controller => 'login'
end
else
redirect_to :controller => 'login'
end
false #stop processing request
else
true #continue
end
end

Reply by ParagramStudios 839 days ago

Which file does this go in? I'm *very* new to rails.

Reply by ParagramStudios 839 days ago

Application.rb?

Reply by Rich Collins 839 days ago

You would replace the action that is currently doing the redirect to /user/login with this code. Most likely application.rb.

Chat Conversation 839 days ago

hey there Rich Collins at 12:59 PM on Thursday September 21st, 2006
hey Rich Collins at 1:30 PM on Thursday September 21st, 2006
are you there now? Rich Collins at 1:30 PM on Thursday September 21st, 2006
yeah, thanks for getting back to me ParagramStudios at 1:30 PM on Thursday September 21st, 2006
don't worry - we are working on a better chat system ;) Rich Collins at 1:31 PM on Thursday September 21st, 2006
sure Rich Collins at 1:31 PM on Thursday September 21st, 2006
What questions do you have about the solution? Rich Collins at 1:31 PM on Thursday September 21st, 2006
i figured this would be a great place to ask rails questions because guruza was built in rails... ParagramStudios at 1:31 PM on Thursday September 21st, 2006
it is indeed Rich Collins at 1:31 PM on Thursday September 21st, 2006
i'm going to try putting it in application.rb, just saw your response a minute ago ParagramStudios at 1:32 PM on Thursday September 21st, 2006
ah ok Rich Collins at 1:32 PM on Thursday September 21st, 2006
well how are you currently redirecting people based on the session? Rich Collins at 1:32 PM on Thursday September 21st, 2006
my code won't work unless you have defined an authenticated? method Rich Collins at 1:32 PM on Thursday September 21st, 2006
it could be as simple as session[:user_id] Rich Collins at 1:33 PM on Thursday September 21st, 2006
and of course, that will redirect you for ALL requests for which you are not authenticated Rich Collins at 1:34 PM on Thursday September 21st, 2006
you will need to modify the filter Rich Collins at 1:34 PM on Thursday September 21st, 2006
that's the error i was just about to tell you about. where should i define authenticated? ParagramStudios at 1:34 PM on Thursday September 21st, 2006
so that it applies only to the actions that the user must be authenticated for Rich Collins at 1:34 PM on Thursday September 21st, 2006
you can define it in application.rb Rich Collins at 1:34 PM on Thursday September 21st, 2006
but you must already be checking for authentication somewhere Rich Collins at 1:34 PM on Thursday September 21st, 2006
as you are redirecting ppl to the login page Rich Collins at 1:35 PM on Thursday September 21st, 2006
still there? Rich Collins at 1:38 PM on Thursday September 21st, 2006
yeah, distracted by other things... just about to test your method with this added: def authenticated? if session[:user_id].nil? false else true end end ParagramStudios at 1:45 PM on Thursday September 21st, 2006
can we chat here? http://jackturner.campfirenow.com/9eea2 ParagramStudios at 1:51 PM on Thursday September 21st, 2006

Reply by ParagramStudios 839 days ago

Thanks Rich, I've got it working now (thanks to a colleugue who knows more rails). I just have one more question about your solution, how can I avoid seeing the redirect javascript that is loaded into the partial before the redirect takes effect? Right now you can see:

Try { window.location.href = "/user/login"; } catch (e) { alert('RJS error:\n\n' + e.toString()); alert('window.location.href = \"/user/login\";'); throw e }

in the partial before the code moves you to /user/login. I'm using ajax updaters right now. Do I have to use ajax.request and process the response? I hope not.

Reply by Rich Collins 839 days ago

Hmm it shouldn't load any visible code into the target. The javascript redirect is working? Let me know if you want to chat again and we can use campfire. Sorry that our chat "needs some work" :(

Reply by ParagramStudios 839 days ago

I hope you can get back to me about the question I asked in the comment above, but your code did lead the code we used to fix this problem so you win the cash. Thanks again.

Reply by Rich Collins 839 days ago

I don't know the prototype library inside and out, but I think the fact that there is an :update specified means that it will load whatever response it gets into the target. If you remove the :update it would probably work. However, you would then have to use RJS to update the element instead of partial (a less than optimal choice). I think it would be hard to get that last bit done without digging into it myself.