Rails 3:如何在Ajax调用中“redirect_to”?

时间:2011-03-28 04:46:29

标签: ruby-on-rails ajax ruby-on-rails-3 redirect

在提交登录表单后,使用Ajax调用以下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

问题是redirect_to不起作用。

你会如何解决这个问题?

7 个答案:

答案 0 :(得分:100)

最后,我刚刚更换了

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

用这个:

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

它工作正常!

答案 1 :(得分:63)

有一种非常简单的方法可以为下一个请求保留闪光灯。在您的控制器中执行类似

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

flash.keep将确保为下一个请求保留闪光灯。 因此,当呈现root_path时,它将显示给定的flash消息。 Rails太棒了:)

答案 2 :(得分:27)

我认为这稍微好一些:

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

答案 3 :(得分:26)

在我的一个应用程序中,我使用JSON来继续重定向和flash消息数据。它看起来像这样:

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

简单的jQuery示例是:

$.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.
  }
})

答案 4 :(得分:18)

结合最佳答案:

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

答案 5 :(得分:1)

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

答案 6 :(得分:0)

我不想修改我的控制器操作,所以我提出了这个黑客攻击:

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