设计:在错误的令牌上覆盖输出到自定义json

时间:2013-02-03 16:02:50

标签: ruby-on-rails-3 devise

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

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

{ “email”:“”, “password”:null }

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

WHY ??我可以覆盖这种行为?我希望在这种情况下显示自定义json

P.S。我正在使用不带会话的令牌身份验证

1 个答案:

答案 0 :(得分:1)

自定义failureApp帮助了我。

class CustomFailureApp&lt;设计:: FailureApp   def回应     if request.format       json_failure     其他       超     结束   端

def json_failure     self.status = 401     self.content_type ='json'     self.response_body =“{'state':{'code':1,'messages'= ['authentication error']}}”//&lt; - 此行确实对我有所帮助   结束 端