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