2014-04-30 38 views
0

我是新来Ruby on RailsRails的:检查验证时,JSON请求

在我的项目,我请求得到indexJSONHTML格式。但我想检查authenticationHTML请求只有不JSON请求。

我用devise宝石,为authentication

Controller我设置

skip_before_action :authenticate_user!, only[:index], if: -> { request.format.json? } 

def index 
    @products = Product.all 
    respond_to do |format| 

    format.html 
    format.json { render :json => @products } 
    end 
end 

目前JSONHTML要求不要求authentication

如何实现这一目标?

有没有什么办法来检查authenticationformat.html { ... }

在此先感谢

回答

0

试试这个:

skip_before_action :authenticate_user!, if: :skip_authenticate_user 

    def index 
    ... 
    end 

    protected 
    def skip_authenticate_user 
     action_name=='index' && (request.format.json? || request.format.xml?) 
    end