Get server-side controller method to be called via plugin route

Please, correct me if I’m wrong, but this has to do with this little line at the top of the controller:

skip_before_action :check_xhr

Adding it to a controller will achieve the desired outcome of having a server-side-only route.

Is it possible to only skip this particular before action for requests with a certain content type (e.g. HTML)?


Update: My current solution works like this:

  1. The before action check_xhr will be skipped
  2. A new before action check_xhr_for_documents is added. It explicitly calls check_xhr when some condition is true (e.g. the request format is HTML)
class PluginRoutesController < ApplicationController
  before_action :check_xhr_for_documents
  skip_before_action :check_xhr

  def show
    Rails.logger.info '┌────────────┐'
    Rails.logger.info '│ Here we go │'
    Rails.logger.info '└────────────┘'
  end

  private

  def check_xhr_for_documents
    if request.format == 'text/html'
      check_xhr
    end
  end
end
2 Likes