質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

解決済

1回答

898閲覧

railsでroutingエラー

MITSUW0

総合スコア11

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

1クリップ

投稿2019/05/22 09:49

前提・実現したいこと

ここに質問の内容を詳しく書いてください。
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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。
フォルダの構成がずれていただけでした、、、

投稿2019/05/22 10:26

MITSUW0

総合スコア11

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問