showメソッドを使った投稿詳細ページ
ここでは詳細ページのshowメソッドだけではなく編集と削除機能も追加
officies_controller.rb
def show
@office = Office.find_by(id: params[:id])
render json: { 'office': @office }
end
def edit
@office = Office.find_by(id: params[:id])
end
def destroy
@office = Office.find_by(id: params[:id])
@office.destroy
render json: {'office': @office}
end
ルーティングはresourcesをofficiesにする。
routes.rb
Rails.application.routes.draw do
namespace :api do
namespace :v1 do
mount_devise_token_auth_for 'User', at: 'user_auth', controllers: {
registrations: 'api/v1/registrations'
# コントローラーの参照先を設定
}
get 'cities', to:'cities#get'
#省略
get ':area_id/:prefecture_id/cities', to:'cities#get'
resources :offices
end
end
end