Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:在json请求时检查身份验证_Ruby On Rails_Ruby_Json_Authentication_Devise - Fatal编程技术网

Ruby on rails Rails:在json请求时检查身份验证

Ruby on rails Rails:在json请求时检查身份验证,ruby-on-rails,ruby,json,authentication,devise,Ruby On Rails,Ruby,Json,Authentication,Devise,我是新来的rubyonrails 在我的项目中,我请求在index上获取JSON和HTML格式。但是我只想检查authentication,查看HTML请求,而不是JSON请求 我使用了designegem,用于身份验证 在我的控制器上设置 skip_before_action :authenticate_user!, only[:index], if: -> { request.format.json? } def index @products = Product.all r

我是新来的
rubyonrails

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

我使用了
designe
gem,用于
身份验证

在我的
控制器上设置

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
当前
JSON
HTML
请求不要求
身份验证

我如何做到这一点

有没有办法在
format.html{…}

提前感谢

试试这个:

  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