2012-08-01 51 views
0

我有一个设计registrations_controller其创建方法重新定向用户经过一些试验。麻烦的是我使用相同的创建方法专用注册页面和灯箱注册。灯箱使用ajax调用来调用create方法,重定向会产生麻烦。Rails的设计注册几次失败的试用后重定向用户

除了调用超级方法之外,我还没有在创建方法上做过很多工作。我如何告诉设计,如果一个Ajax的调用,不要重定向?

感谢

回答

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 

配置监狱长使用此故障的应用程序在色器件初始化:

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/