RailsでAPIを作るときのネストしたルーティングとコントローラーの書き方
Ruby on RailsでAPIを作ろうとして /app/controllers/api/v1/hoge_person_controller.rb というコントローラーを用意して、getでアクセスしたらJsonを返すようにする。
class Api::V1::HogePersonController < ApplicationController before_action :set_hoge_person, only: [:profile] def profile render json: @hoge_person end private def set_hoge_person @hoge_person = {name: 'Tom', age: 20} end end
/config/routes.rbは下記のような感じ
get 'api/signup' => 'api/v1/hoge_person#profile'
参考
https://qiita.com/ko_coon/items/a23b3a95fa29c967257f
https://qiita.com/kogache/items/159d7302c4b61f606490