前提・実現したいこと
ここに質問の内容を詳しく書いてください。
RailsのAPIモードでHTTP通信を実装したいのですが、
参考にしたサイト(https://qiita.com/k-penguin-sato/items/adba7a1a1ecc3582a9c9)をもとに
ルーティングやコントローラーを記述し、POSTMANを用いてGETリクエストをしたところ、ルーティングエラーが発生してしまいました。
ソースコードやエラーメッセージから、解決方法を教えていただきたいです。
POSTMANに入力したリクエストURL
http://localhost:3000/api/v1/spots
rake routesコマンドによる出力
(上部略) Prefix Verb URI Pattern Controller#Action api_v1_spots GET /api/v1/spots(.:format) api/v1/spots#index POST /api/v1/spots(.:format) api/v1/spots#create api_v1_spot GET /api/v1/spots/:id(.:format) api/v1/spots#show PATCH /api/v1/spots/:id(.:format) api/v1/spots#update PUT /api/v1/spots/:id(.:format) api/v1/spots#update DELETE /api/v1/spots/:id(.:format) api/v1/spots#destroy rails_service_blob GET /rails/active_storage/blobs/:signed_id/*filename(.:format) active_storage/blobs#show rails_blob_representation GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format) active_storage/representations#show rails_disk_service GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show update_rails_disk_service PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update rails_direct_uploads POST /rails/active_storage/direct_uploads(.:format) active_storage/direct_uploads#create
POSTMANエラーメッセージ
{ "status": 404, "error": "Not Found", "exception": "#<ActionController::RoutingError: uninitialized constant Api>", "traces": { "Application Trace": [], "Framework Trace": [ { "id": 0, "trace": "vendor/bundle/ruby/2.3.0/gems/bootsnap-1.4.2/lib/bootsnap/load_path_cache/core_ext/active_support.rb:79:in `block in load_missing_constant'" }, (略)
該当のソースコード(routes.rb)
ruby
1Rails.application.routes.draw do 2 # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html 3 namespace 'api' do 4 namespace 'v1' do 5 resources :spots 6 end 7 end 8end
該当のソースコード(routes.rb)
ruby
1module Api 2 module V1 3 class SpotsController < ApplicationController 4 def index 5 spots = Spots.order(created_at: :desc) 6 render json: { status: 'SUCCESS', message: 'loaded Spots', data: spots } 7 end 8 9 def show 10 spot = Spot.find(params[:id]) 11 render json: { status: 'SUCCESS', message: 'loaded the spot', data: spot } 12 end 13 14 def create 15 spot = Spot.new(spot_params) 16 if spot.save 17 render json: { status: 'SUCCESS', message: 'loaded the spot', data: spot } 18 else 19 render json: { status: 'ERROR', message: 'spot not saved', data: spot.errors } 20 end 21 22 def destroy 23 spot = Spot.find(params[:id]) 24 spot.destroy 25 render json: { status: 'SUCCESS', message: 'deleted the spot', data: spot } 26 end 27 28 def update 29 spot = Spot.find(params[:id]) 30 if spot.update(spot_params) 31 render json: { status: 'SUCCESS', message: 'updated the spot', data: spot } 32 else 33 render json: { status: 'SUCCESS', message: 'loaded the spot', data: spot } 34 end 35 end 36 37 private 38 39 def spot_params 40 params.require(:spot).permit(:name) 41 end 42 end 43 end 44end 45
補足情報
Rails: 5.2.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。