Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4000048
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:44:00+00:00 2026-05-20T07:44:00+00:00

With the following code : class TestsController < ApplicationController skip_before_filter :load_something, # how to

  • 0

With the following code :

class TestsController < ApplicationController
  skip_before_filter :load_something,      # how to skip these only 
                     :load_something_else  # if the request is xhr ?

  def index
    respond_to do |format|
      format.html
      format.js
    end
  end

end

Is there a way to skip before filters depending if the request is a javascript call without changing the :load_something and :load_something_else methods ?

Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:44:01+00:00Added an answer on May 20, 2026 at 7:44 am

    There used to be no clean solution to this. Assuming you do not have control over the before_filters (or do not want to change them), i would leave the skip_before_filter and add an extra before_filter that only executes the wanted methods if it is an xhr method.

    So something like

    skip_before_filter :load_something,      # how to skip these only 
                       :load_something_else  # if the request is xhr ?
    before_filter :do_loads_if_xhr
    
    def do_loads_if_xhr
      if request.xhr?
        load_something
        load_something_else
      end
    end
    

    The method described here only works because the conditional is global for the application: the conditional is only evaluated when the class is created. Not on each request.

    [UPDATED]
    There is however, a cleaner way to make the skip_before_filter conditional. Write it like this:

    skip_before_filter :load_something, :load_something_else, :if => proc {|c| request.xhr?}
    

    Also note that while skip_before_filter is still completely supported, and not deprecated, since rails 4 the documentation seems to prefer the (identical) skip_before_action. Also the documentation is totally not clear about this, had to verify in code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take the following code: class ChallengesController < ApplicationController def update @challenge = Challenge.find(params[:id]) @challenge.update!(params[:challenge])
The following code: class Log: BAT_STATS = ['AB', 'R', 'H', 'HR'] def __init__(self, type):
The following code: class MyClass(): def test(self): self.__x = 0 def __setattr__(self, name, value):
I have following code: class EntityBase (object) : __entity__ = None def __init__ (self)
I have the following code: class IncidentTag: def __init__(self,tag): self.tag = tag def equals(self,obj):
With the following code: class ObjA def func puts ObjA end end module Mod
Imagine the following code: class SimpleLetter def values (a .. z).to_a end def ===(other)
I have the following code: class Engine attr_accessor :isRunning def initialize @isRunning = false
I have the following code: class A(object): def __init__(self): self.name = A super(A, self).__init__()
The following code class Schedule < ActiveRecord::Base def self.get today = TZInfo::Timezone.get('America/New_York').utc_to_local(Time.now.utc).to_date end end

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.