gpt4 book ai didi

ruby-on-rails - rails 3.2.0 仅当它是 json 格式时才在过滤器之前跳过

转载 作者:行者123 更新时间:2023-12-01 10:43:54 24 4
gpt4 key购买 nike

1. skip_before_filter :authorize, :only => [:index,:show], :if => proc {|c| request.json?}

2. skip_before_filter :authorize, :only => [:index,:show], :if => :format_json?

def format_json?
request.format.json?
end

3. skip_before_filter :authorize, :only => [:index, :show] , :if =>request.content_type == "json"
for the first request it is showing:
#undefined local variable or method `request' for ExamsController:Class
If i refresh the page it is not skipping anyting

仅当请求内容为 json 时,我才想跳过“索引”和“显示”操作的身份验证。我试过上面的例子,但没有任何效果。任何建议都会有所帮助。

最佳答案

class XXXXController < ApplicationController
skip_before_filter :authorize, :only => [:index,:show], if: :json_request?

#your other actions here.

private
def json_request?
request.format.symbol == :json
end
end

在我的例子中,我尝试了以下对我的操作的调用,并且格式被验证为 :json 类型:

$(document).on('ready', function(){

$('body').on('submit', '#new_job', function(e){
e.preventDefault();

$.ajax({
dataType: "json",
url: '/jobs/',
type: 'post',
success: function(data){
console.log("registered success");
},
failure: function(error){
console.log("This is the failure block ");
}
});
});
});

enter image description here

关于ruby-on-rails - rails 3.2.0 仅当它是 json 格式时才在过滤器之前跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28271566/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com