Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Skip Before Filter on API Call

Is there any way to skip the before filter for an action when an API call is being made?

So generally I want before_filter x to be executed EXCEPT when I'm doing an API call.

skip_before_filter :x, :except => API CALL BEING MADE
like image 249
RuneScape Avatar asked Sep 16 '25 17:09

RuneScape


2 Answers

It depends a little in how you define an API call. If it's any request with a JSON format you could do something like this:

skip_before_action :x if: -> { request.format.json? }

This doesn't seem to be an ideal solution though, and it might be better to make separate controllers for Web and API. You might do something along these lines:

class APIController < ApplicationController
     # API-specific filters
end

class SomeOtherController < APIController
    # your API actions
end

And you'd do the same for web, then configure your routes to take the request to the correct controller action.

Hope that helps.

like image 69
Roma149 Avatar answered Sep 18 '25 08:09

Roma149


In your controller you can add

    skip_before_action :x, only: WHEN API CALL BEING MADE

I guess, you would want only and not except

like image 42
Sudipta Mondal Avatar answered Sep 18 '25 09:09

Sudipta Mondal