1
I want to show all models by request http://server/api/art/show.json?auth_token=correct_token
class ArticlesController < ApplicationController

  before_filter :authenticate_user!

  def show
    arts = Article.all
    render :json => {:state => {:code => 0}, :data => arts}
  end

  def new

  end
end

如果令牌是正确的,那么我会收到数据并且没关系,但如果令牌不正确,我有:

{“电子邮件”:“”,“密码”:空}

In logs i see that it redirected me to http://server/users/sign_in.json 

为什么??我可以在哪里覆盖这种行为?在这种情况下,我想显示自定义 json

PS我正在使用没有会话的令牌身份验证

4

1 回答 1

1

自定义 failureApp 帮助了我。

class CustomFailureApp < Devise::FailureApp def respond if request.format json_failure else super end end

def json_failure self.status = 401 self.content_type = 'json' self.response_body = "{'state' : {'code' : 1, 'messages' = ['authentication error'] } }" // <- 这一行确实帮助我结束了

于 2013-02-05T16:26:30.413 回答