2012-08-01 47 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/