我有一个设计registrations_controller,其创建方法经过几次试验后重定向用户。问题是我使用相同的创建方法进行专用注册页面和灯箱注册。灯箱使用ajax调用调用create方法,重定向会造成麻烦。
除了调用super之外,我在create方法中没有做太多。如果调用ajax调用它怎么告诉devise不重定向?
由于
答案 0 :(得分: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/