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
我想跳过" index"的身份验证并且"显示"仅当请求内容为json时才执行操作。我尝试了上面的例子,但没有任何工作。任何建议都会有所帮助。
答案 0 :(得分:2)
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
在我的情况下,我尝试跟随我的行动&amp;格式经验证为:json type:
$(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 ");
}
});
});
});
答案 1 :(得分:2)
这应该解决它
skip_before_filter :authorize, :only => [:index,:show], :if => Proc.new {|c| c.request.format.json?}
您需要执行Proc.new,然后使用本地变量c并访问格式。
答案 2 :(得分:0)
对于Rails 3,您想这样做
class XXXXController < ApplicationController
skip_before_filter :authorize, if: :public_request?
#your other actions here.
private
def public_request?
# action_name is a rails method that returns the requested action
public_action = (action_name == 'show' or action_name == 'index' )
return ( public_action and request.format.symbol == :json )
end
end