Rails 3: How is "redirect_to" in an Ajax call?

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

 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 not working.

How would you solve this?

+72
redirect ajax ruby-on-rails ruby-on-rails-3
Mar 28 2018-11-11T00:
source share
7 answers

Finally, I just replaced

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

with this:

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

and it works great!

+84
Mar 29 '11 at 10:29
source share

There is a very simple way to save flash for the next request. In the controller, do something like

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

flash.keep will ensure that the flash is saved for the next request. Therefore, when root_path displayed, it displays this flash message. Rails is awesome :)

+49
Jul 17 '13 at 0:35
source share

I think this is a little better:

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

+26
Apr 20 2018-12-14T00:
source share

In one of my applications, I use JSON to transfer redirect data and flash messages. It will 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 a simple jQuery example:

 $.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. } }) 
+23
Mar 28 '11 at 5:02
source share

Combination of the best 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 ... 
+15
Aug 26 '13 at 12:22
source share
 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 
+1
Oct 27
source share

I did not want to change my actions with the controller, 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 
0
Oct 15 '14 at 3:27
source share



All Articles