cor*_*eyt 8 json ruby-on-rails devise
我的环境是带有Devise 1.1的Rails 3.0.1.我正在开发一个移动Web应用程序,主要使用javascript,并且希望尽可能多地保持基于JSON的通信.
有没有办法让设计用JSON响应成功/失败消息,而不是必须遵循302重定向并解析HTML?
看着用这个.
......但是没有它工作.
您可以重新定义sign_in_and_redirect和sign_out_and_redirect设计帮助程序,以呈现json而不是重定向用户.
我无法在初始化程序中重新定义它们,因此我找到的最接近的解决方案是将其添加到application_controller.rb:
private
# Override the default devise signin/signout process
def sign_in_and_redirect(resource_or_scope, resource=nil)
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
# redirect_to stored_location_for(scope) || after_sign_in_path_for(resource)
render :json => {:status => :signed_in}
end
def sign_out_and_redirect(resource_or_scope)
scope = Devise::Mapping.find_scope!(resource_or_scope)
if Devise.sign_out_all_scopes
sign_out_all_scopes
else
sign_out(scope)
end
#redirect_to after_sign_out_path_for(scope)
render :json => {:status => :signed_out}
end
Run Code Online (Sandbox Code Playgroud)
如果有人有更清洁的解决方案,我也很感兴趣