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

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

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

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

Ruby on Rails

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

API

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

Q&A

0回答

574閲覧

railsでAPIを複数作成しているのですが、ルーティングが無視されてしまい、うまく反映されません。

ganbarou_nippon

総合スコア18

Ruby

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

Ruby on Rails

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

API

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

1グッド

1クリップ

投稿2020/04/07 01:35

railsでgrapeを使ってAPIを作成しているのですが、V1,V2二つのAPIのうちV2だけがルーティングに反映されません。

エラーも出ていないのでどうすれば良いかわからず困っています。

v1はdeviseの認証に関する部分です。

v2はその他のAPIです。

ruby

1config/routes.rb 2Rails.application.routes.draw do 3 #mount_devise_token_auth_for 'User', at: 'auth' 4 # standard devise routes available at /users 5 # NOTE: make sure this comes first!!! 6 devise_for :users 7中略 8 namespace :apis do 9 namespace :api do 10 mount_devise_token_auth_for 'User', at: '/v1/auth' 11 mount API::Root => '/' 12 end 13 # token auth routes available at /api/v1/auth 14 end 15end

ルーティングはこのような記述です。

ファイル構成は

イメージ説明

このような感じです。

ruby

1apis/api/v2/root.rb 2module API 3 module V2 4 class Root < Grape::API 5 version 'v2', using: :path 6 format :json 7 8 mount API::V2::Users 9 mount API::V2::Notes 10 mount API::V2::Comments 11 mount API::V2::Replies 12 end 13 end 14end

ruby

1apis/api/v1/root.rb 2module API 3 module V1 4 class Root < Grape::API 5 version 'v1', using: :path 6 format :json 7 8 class ApplicationController < ActionController::API # Note: here is not ::BASE 9 include DeviseTokenAuth::Concerns::SetUserByToken 10 end 11 12 auth :grape_devise_token_auth, resource_class: :user 13 helpers GrapeDeviseTokenAuth::AuthHelpers 14 15 desc 'GET apis/api/v1/test' 16 get 'test' do 17 authenticate_user! 18 { 19 message: 'test', 20 current_user_uid: current_user.uid, 21 authenticated?: authenticated?, 22 } 23 end 24 end 25 end 26end

ruby

1apis/api/root.rb 2module API 3 class Root < Grape::API 4 GrapeDeviseTokenAuth.setup! do |config| 5 config.authenticate_all = true 6 end 7 prefix 'api' 8 mount API::V1::Root 9 mount API::V2::Root 10 end 11end

またv2モデルのファイルはcomments.rbを例に出すとこのような感じです。

ruby

1api/api/v2/comments.rb 2module API 3 module V2 4 class Comments < Grape::API 5 resources :comments do 6 7 desc 'Create a comment' 8 params do 9 requires :id, type: Integer 10 requires :text, type: String 11 requires :note_id, type: Integer 12 end 13 post "/" do 14 @comment=Comment.create(text: params[:text],note_id: params[:note_id]).merge(user_id: current_user.id) 15 end 16 17 desc 'Edit a comment' 18 params do 19 requires :id, type: Integer 20 end 21 get '/:id/edit' do 22 @comment=Comment.find(params[:id]) 23 end 24 25 desc 'Update a comment' 26 params do 27 requires :id, type: Integer 28 requires :text, type: String 29 requires :note_id, type: Integer 30 end 31 patch '/:id' do 32 if current_user 33 comment=Comment.find(params[:id]) 34 comment.update(text: params[:text],note_id: params[:note_id]).merge(user_id: current_user.id) 35 end 36 end 37 38 desc 'Delete a comment' 39 params do 40 requires :id, type: Integer 41 end 42 delete '/:id' do 43 @comment=Comment.find(params[:id]) 44 if @comment.user_id == current_user.id 45 @comment.destroy 46 end 47 end 48 49 private 50 def comment_params 51 requires :id, type: Integer 52 requires :text, type: String 53 requires :note_id, type: Integer 54 end 55 end 56 end 57 end 58end

また、ファイルの読み込みに関するapplication.rbの記述は以下です。

ruby

1config/application.rb 2 3module Tradenote 4 class Application < Rails::Application 5 # Initialize configuration defaults for originally generated Rails version. 6 config.load_defaults 5.2 7 config.i18n.default_locale = :ja 8 config.assets.initialize_on_precompile = false 9 config.paths.add File.join('app', 'apis'), glob: File.join('**', '*.rb') 10 config.autoload_paths += Dir[Rails.root.join('app', 'apis', '*')] 11 end 12end

このようなコードで書いているのですが、

rake routes とすると、

イメージ説明

画像のようにv2に関するルーティングが完全に無視されてしまいます。

どう対応すれば良いでしょうか?

ご教授いただければ幸いです。

ご対応の方よろしくお願い申し上げます。

DrqYuto👍を押しています

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問