【问题标题】:rails 3.2.0 skip before filter only if it is a json format只有当它是 json 格式时,rails 3.2.0 才会在过滤器之前跳过
【发布时间】:2015-02-02 05:24:11
【问题描述】:
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 时,我才想跳过“索引”和“显示”操作的身份验证。我已经尝试了上述示例,但没有任何效果。任何建议都会有所帮助。

【问题讨论】:

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


    【解决方案1】:
    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 ");
          }
        });
       });
    });
    

    【讨论】:

    • 谢谢,但它不起作用。它正在跳过 :authorize 所有格式。是否允许在 rails 3.2.0 中跳过特定格式的身份验证?
    • 我收到警告“警告无法确定响应正文的内容长度。设置响应的内容长度或设置 Response#chunked = true”。这可能会导致任何问题?
    • 好吧,就我而言,它没有显示任何此类警告。请您分享您发送json请求类型的代码..
    • @HabibullahAraphatKonok,我已经更新了我的答案。看看这就是我发送我的 json 类型的请求并在 before_action 中验证相同的方式。请分享代码部分,哪个部分正在发送您的 json 类型的请求。
    • 感谢@martin 经过反复试验,以下代码对我有用。我不确定这种方法有什么问题。 before_filter :authorize, :except =&gt; [:index,:show]before_filter(:only =&gt; [:index,:show]) do |controller|authorize unless controller.request.format.json?end
    【解决方案2】:

    这应该可以解决它

    skip_before_filter :authorize, :only =&gt; [:index,:show], :if =&gt; Proc.new {|c| c.request.format.json?}

    您需要执行 Proc.new,然后使用局部变量 c 并访问格式。

    【讨论】:

      【解决方案3】:

      对于 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-23
        • 2019-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-08
        相关资源
        最近更新 更多