2013-02-03 32 views
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 

如果令牌是正確的,那麼我收到的數據,它是確定的,但如果令牌是不正確的,我有:設計:覆蓋輸出定製JSON的錯誤令牌

{ 「電子郵件」:「」 , 「密碼」:空 }

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

爲什麼?我可以覆蓋這種行爲?我想在這種情況下顯示自定義JSON

P.S.我正在使用令牌身份驗證沒有會話

+0

此博客文章可能有用:http://jessewolgamott.com/blog/2012/01/19/the-one-with-a-json-api-login-using-devise/ – mccannf

+0

我看到了,我有類似的代碼,並找不到爲什麼它不起作用。 – NeverBe

回答

1

自定義failureApp幫助我。

類CustomFailureApp <設計:: FailureApp 高清迴應 如果request.format json_failure 其他 超級 結束 結束

高清json_failure self.status = 401 self.content_type = 'JSON' self.response_body =「{'state':{'code':1,'messages'= ['authentication error']}}」// < - 此行確實幫助我 結束 結束