I have an Authlogic login form with :remote => true
that does a little inline validation with an RJS template if the user/password isn't valid. This works fine, but when the credentials are valid, it doesn't properly redirect elsewhere.
Here's the controller that responds to the form input:
class UserSessionsController < ApplicationController
respond_to :html, :js
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => :destroy
def new
@user_session = UserSession.new
end
def create
@user_session = UserSession.new(params[:user_session])
respond_to do |format|
if @user_session.save
flash[:notice] = "Login successful!"
format.html { redirect_to account_url }
else
format.js
end
end
end
def destroy
current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_to root_path
end
end
The format.js
part works but if the user/password are good (format.html
), nothing happens. However, if I look a development.log, it is requesting the account_url page. It's just not redirecting you in the browser. I think it's returning the account page via AJAX and I really just want a normal redirect.
The HTML for the form is this:
<%= simple_form_for(
:user_session, @user_session,
:url => { :controller => 'user_sessions', :action => "create" },
:html => { :id => 'login-dropdown' }, :remote => true) do |f| %>
I found a way to fix it. Per http://www.ruby-forum.com/topic/168406#945053, I added the following to my application controller:
def redirect_to(options = {}, response_status = {})
if request.xhr?
render(:update) {|page| page.redirect_to(options)}
else
super(options, response_status)
end
end
This prevents the redirect response from being delivered via xhr. If there's a more "correct" way of doing this in the controller, I'd like to hear it, though.