Using ActionController::API for just API controllers

250 views
Skip to first unread message

Yos Riady

unread,
Apr 13, 2015, 5:21:22 AM4/13/15
to rails-a...@googlegroups.com
I want to use the thin middleware layer for my API controllers, but I still need dashboard views for my application. 
I have a Base APIController:

class API::APIController < ApplicationController

end

Which provides some base auth methods to actual API resource controllers:

class API::V1::AgentsController < API::APIController

end

Using rails-api, I'd like to still generate views for my other controllers, yet still benefit from the thinner middleware for my API Controllers, so can I do the following:

class ApplicationController < ActionController::Base
end

class API::APIController < ActionController::API
end

All of my non-API controllers will simply inherit from ApplicationController and be able to generate views as per normal, while my API controllers enjoy better performance. Would this work?

Thanks for the advice.

Yos Riady

unread,
Apr 13, 2015, 5:23:50 AM4/13/15
to rails-a...@googlegroups.com
I forgot to mention that this would mean that the project is generated by normal Rails, with rails-api gem installed.

Carlos Antonio da Silva

unread,
Apr 13, 2015, 6:55:14 AM4/13/15
to Yos Riady, rails-a...@googlegroups.com
I'd not recommend using rails-api if your application is serving as a webapp for example, it's meant to be used by api-only apps, because it changes the app middleware, which would affect every request. Just go with Rails and you'll be fine.

--
You received this message because you are subscribed to the Google Groups "rails-api-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rails-api-cor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
At.
Carlos Antonio

Benjamin Fleischer

unread,
Jun 4, 2015, 11:00:14 AM6/4/15
to rails-a...@googlegroups.com
There's no rule that says you can't have ApplicationController < ActionController::Base and APIController < ActionController::API in the same application.... or that you even need
rails-api if your app has the full rails stack.  Though I think you should pick one, since they both define Rails::Application https://github.com/rails-api/rails-api/blob/67115bc06ee388db5b8e70822553db686745097c/lib/rails-api/application.rb#L5-L6 https://github.com/rails/rails/blob/fd9169ff7e74c4a652886a6df259dabc5962802e/railties/lib/rails/application.rb#L80 and note that Rails api is being merged into Rails 5 https://github.com/rails/rails/pull/19832

And some practical code:


later
-Benjamin


Samnang Chhun

unread,
Jun 5, 2015, 12:22:32 AM6/5/15
to Benjamin Fleischer, rails-a...@googlegroups.com
Currently, here what I do in my project to get it supports for both:

# Gemfile
gem 'rails-api', require: false


# app/controllers/api/v1/base_controller.rb
require 'rails-api/action_controller/api'

class API::V1::BaseController < ActionController::API
  ...
end

- Samnang


--
You received this message because you are subscribed to the Google Groups "rails-api-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rails-api-cor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages