0

我有一个设计 registrations_controller,它的 create 方法在几次试验后重定向用户。问题是我对专用注册页面和灯箱注册使用相同的创建方法。灯箱使用 ajax 调用调用 create 方法,并且重定向会造成麻烦。

除了调用 super 之外,我在 create 方法中没有做太多事情。如果对它进行 ajax 调用,我如何告诉设计不要重定向?

谢谢

4

1 回答 1

1

为了防止重定向,您可以构建一个自定义失败应用程序:

 class CustomFailureApp < Devise::FailureApp

  def respond    
    unless request.format.to_sym == :html
      http_auth
    else
      redirect_to redirect_url
   end
    end

   def http_auth 
      super     
      self.status         = 401
      self.content_type   = 'json'
      self.response_body  = {
        :error => 'NO MORE REDIRECT',
        :status => 401
      }.to_json
  end
end

配置 Warden 以在设计初始化程序中使用此失败应用程序:

    Devise.setup do |config|

  ...

  # ==> Warden configuration
  # If you want to use other strategies, that are not (yet) supported by Devise,
  # you can configure them inside the config.warden block.
  config.warden do |manager|
    manager.failure_app = CustomFailureApp
  end
end

这是一个参考:

http://casperfabricius.com/site/2010/09/30/ajax-sign-in-and-sign-up-with-devise/

于 2012-08-01T01:55:41.020 回答