【Rails】APIモードで作成したあとに一部機能で画面を作成する方法
2020/11/13
RailsアプリをAPIモードで新規作成(rails new --api)して、一部機能だけ画面を作成したい時のやり方。
対象のコントローラーでApplicationControllerではなく、ActionController::Baseを継承させてあげれば良い。
あとは通常通り、Viewを用意するだけ。
app/controllers/articles_controller.rb
-class ArticlesController < ApplicationController
+class ArticlesController < ActionController::Base
def feed
@articles = Article.all
end
endAPIモードで作成すると、ApplicationControllerは不要な機能を省いたAPI用のActionController::APIを継承している。
app/controllers/application_controller.rb
class ApplicationController < ActionController::API
end