class Users:: SessionsController < Devise::SessionsController def create respond_to do |format| format.HTML { super } format.Json { warden.authenticate!(:scope => resource_name,:recall => "#{controller_path}#failure") render :Json => {:success => true} } end end def destroy respond_to do |format| format.HTML {super} format.Json { Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name) render :Json => {} } end end def failure render :Json => {:success => false,:errors => ["Login Failed"]} endend
这很好,但是当身份验证失败时,失败并不会返回Json失败.我有一个定制的设计失败.如果我删除redirect_url或完全删除客户故障,则身份验证将返回带有失败消息的Json.我的自定义失败如下:
class CustomFailure < Devise::FailureApp def redirect_url #return super unless [:worker,:employer,:user].include?(scope) #make it specific to a scope '/' end # You need to overrIDe respond to eliminate recall def respond if http_auth? http_auth else redirect endend
结束
无论如何,如果它是一个HTML请求,保持重定向,并返回一个带有失败消息的Json,如果它是一个Json请求?
谢谢!
解决方法 你必须告诉warden使用那个自定义失败.def failure respond_to do |format| format.HTML {super} format.Json do warden.custom_failure! render :Json => {:success => false,:errors => ["Login Failed"]} end endend总结
以上是内存溢出为你收集整理的ruby-on-rails – 通过json设计失败认证发回html而不是json全部内容,希望文章能够帮你解决ruby-on-rails – 通过json设计失败认证发回html而不是json所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)