Grape使いたくなかったときのメモ
なんか新しいディレクトリ掘るじゃないですかー、Grapeってー。
controllers
の中に入れたりしてもいいらしいですけどー、MVCからハミ出してる感じがしてー、なーんか好きじゃないんですよー。
ということを考えたときのメモ。
class PostsController < ApplicationController before_action :set_post, only: [:show, :update, :destroy] def index @posts = Post.all render :index, with_jbuilder end def show render :show, with_jbuilder end def create @post = Post.new(post_params) if @post.save render :show, with_jbuilder(status: :created) else render json: @post.errors, status: :unprocessable_entity end end def update if @post.update(post_params) render :show, with_jbuilder(status: :ok) else render json: @post.errors, status: :unprocessable_entity end end def destroy @post.destroy head :no_content end private def set_post @post = Post.find(params[:id]) end def post_params params.require(:post).permit(:body) end end
class ApplicationController < ActionController::Base # 略 protected def with_jbuilder(options = {}) {formats: [:json], handlers: [:jbuilder]}.merge(options) end end
まあ、そんなたいそうなことはしてないです。