Rails 3: How to "redirect_to" in Ajax call?

Ruby on-RailsAjaxRuby on-Rails-3Redirect

Ruby on-Rails Problem Overview


The following attempt_login method is called using Ajax after a login form is submitted.

class AccessController < ApplicationController
  [...]
  def attempt_login
    authorized_user = User.authenticate(params[:username], params[:password])
    
    if authorized_user
      session[:user_id] = authorized_user.id
      session[:username] = authorized_user.username
      flash[:notice] = "Hello #{authorized_user.name}."
      redirect_to(:controller => 'jobs', :action => 'index')
    else
      [...]
    end
  end
end

The problem is that redirect_to doesn't work.

How would you solve this ?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Finally, I just replaced

redirect_to(:controller => 'jobs', :action => 'index')

with this:

render :js => "window.location = '/jobs/index'"

and it works fine!

Solution 2 - Ruby on-Rails

There is very easy way to keep the flash for the next request. In your controller do something like

flash[:notice] = 'Your work was awesome! A unicorn is born!'
flash.keep(:notice)
render js: "window.location = '#{root_path}'"

The flash.keep will make sure the flash is kept for the next request. So when the root_path is rendered, it will show the given flash message. Rails is awesome :)

Solution 3 - Ruby on-Rails

I think this is slightly nicer:

render js: "window.location.pathname='#{jobs_path}'"

Solution 4 - Ruby on-Rails

In one of my apps, i use JSON to carry on the redirect and flash message data. It would look something like this:

class AccessController < ApplicationController
  ...
  def attempt_login
    ...
    if authorized_user
      if request.xhr?
        render :json => {
          :location => url_for(:controller => 'jobs', :action => 'index'),
          :flash => {:notice => "Hello #{authorized_user.name}."}
        }
      else
        redirect_to(:controller => 'jobs', :action => 'index')
      end
    else
      # Render login screen with 422 error code
      render :login, :status => :unprocessable_entity
    end
  end
end

And simple jQuery example would be:

$.ajax({
  ...
  type: 'json',
  success: functon(data) {
    data = $.parseJSON(data);
    if (data.location) {
      window.location.href = data.location;
    }
    if (data.flash && data.flash.notice) {
      // Maybe display flash message, etc.
    }
  },
  error: function() {
    // If login fails, sending 422 error code sends you here.
  }
})

Solution 5 - Ruby on-Rails

Combining the best of all answers:

...
if request.xhr?
  flash[:notice] = "Hello #{authorized_user.name}."
  flash.keep(:notice) # Keep flash notice around for the redirect.
  render :js => "window.location = #{jobs_path.to_json}"
else
...

Solution 6 - Ruby on-Rails

def redirect_to(options = {}, response_status = {})
  super(options, response_status)
  if request.xhr?
    # empty to prevent render duplication exception
    self.status = nil
    self.response_body = nil
    path = location
    self.location = nil

    render :js => "window.location = #{path.to_json}"
  end
end

Solution 7 - Ruby on-Rails

I didn't want to modify my controller actions so I came up with this hack:

class ApplicationController < ActionController::Base
  def redirect_to options = {}, response_status = {}
    super

    if request.xhr?
      self.status        = 200
      self.response_body = "<html><body><script>window.location.replace('#{location}')</script></body></html>"
    end
  end
end

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMisha MoroshkoView Question on Stackoverflow
Solution 1 - Ruby on-RailsMisha MoroshkoView Answer on Stackoverflow
Solution 2 - Ruby on-RailsnathanvdaView Answer on Stackoverflow
Solution 3 - Ruby on-RailsMikeView Answer on Stackoverflow
Solution 4 - Ruby on-RailsPriitView Answer on Stackoverflow
Solution 5 - Ruby on-RailsYarinView Answer on Stackoverflow
Solution 6 - Ruby on-RailsOlegZView Answer on Stackoverflow
Solution 7 - Ruby on-RailsMacarioView Answer on Stackoverflow