【问题标题】:Rails : Check authentication when json requestRails:json请求时检查身份验证
【发布时间】:2014-04-30 11:49:11
【问题描述】:

我是Ruby on Rails的新手

在我的项目中,我要求在index 上获取JSONHTML 格式。但我只想检查authenticationHTML 请求而不是JSON 请求。

我使用devise gem,作为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

我如何做到这一点?

有什么方法可以在format.html { ... } 上查看authentication

提前致谢

【问题讨论】:

    标签: ruby-on-rails ruby json authentication devise


    【解决方案1】:

    试试这个:

      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
    

    【讨论】:

      猜你喜欢
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多