Reputation: 6609
I am interested in adding an API to an existing rails application. I would like Application controller to inherit from ActionController::Base and Api Controller to inherit from ActionController::API so that the unneeded middleware does not exist on the API endpoints
I can add the rails-api gem (I'm locked on rails 4.2), and adjust the wrap parameters initializer as needed, but the logging of session ids in my rails app breaks when this occurs.
Are there issues with using both rails and rails-api? and Can a rails application have two different bases?
For the sake of discussion, assume that I cannot just make a separate rails-api app and pull the business logic out into gems.
EDIT:
What we have:
ActionController::Base
\ ApplicationController
\ RailsControllers
\ ApiController
\ RailsAPIControllers
What we would like:
ActionController::Base
\ ApplicationController
\ RailsControllers
ActionController::API
\ ApiController
\ RailsAPIControllers
I am not sure if there is any actual difference if the Base ActionController has to be loaded at some point though. The idea is that the RailAPIControllers would be lighter if they inherited from API ActionController, but doing so is causing issues.
Upvotes: 2
Views: 1469
Reputation: 6075
Nathan,
Your best bet is to create an ApiController that all of your api endpoints inherit from that just render json and take care of all of your session/auth stuff. Then you won't have to worry about supporting any weirdness from both gems.
For api you will most likely want to inherit from ActionController::Base UNLESS your session/before action stuff in the api will mimic rails http requests. If the later is the case inherit from ApplicationController.
Upvotes: 1
Reputation: 161
I'm actually in the same boat right now, and as "an exercise" I'm creating a Rails Engine to separate all the API logic/routes/serializers/specs, etc. from our main application. I really just wanted an excuse to try out engines...
I've only just started, but there are some clear advantages and disadvantages that I see already versus just having separate namespaces for my controllers and serializers.
The advantages include isolated dependencies (since it's essentially a gem), separate tests, nice routing (mounted routes in your main application's routes), and built in namespacing for everything involved.
The disadvantage so far that I've run into is duplication. As of right now, I need factories for my specs, but I haven't found a sane way to use the factories from the parent app. For now, I'm just re-creating the factories I need in the engine. I'm sure there's a better way, I just haven't figured it out yet.
Upvotes: 0