簡體   English   中英

Rails命名空間路由到錯誤的Controller

[英]Rails namespaced routes to wrong Controller

所以我剛剛開始使用RoR並且認為我做了一個帶有API端點的基本博客。 問題是我的api請求似乎被路由到錯誤的控制器,

我有以下作為我的routes.rb

Blog::Application.routes.draw do

  namespace :api do
    namespace :v1 do
      resources :articles
    end
  end

end

我還有controllers/api/v1/articles_controller.rb ,它具有以下內容:

module API
  module V1    
    class ArticlesController < ApplicationController
      respond_to :json

      def index
        respond_with Article.all
      end

    end
  end
end

我的邏輯說當我點擊http://localhost:3000/api/v1/articles ,這應該是Controller響應,但是響應的實際控制器是控制器根目錄中的控制器( controllers/articles_controller.rb )而不是/api/v1路徑中的那個。 當我刪除實際響應的Controller時,我將獲得uninitialized constant Api::V1::ArticlesController

甚至rake routes給了我預期的路由,但實際上擊中這些端點失敗了。 rake routes輸出如下:

api_v1_articles GET    /api/v1/articles(.:format)          api/v1/articles#index
                    POST   /api/v1/articles(.:format)          api/v1/articles#create
 new_api_v1_article GET    /api/v1/articles/new(.:format)      api/v1/articles#new
edit_api_v1_article GET    /api/v1/articles/:id/edit(.:format) api/v1/articles#edit
     api_v1_article GET    /api/v1/articles/:id(.:format)      api/v1/articles#show
                    PUT    /api/v1/articles/:id(.:format)      api/v1/articles#update
                    DELETE /api/v1/articles/:id(.:format)      api/v1/articles#destroy

我在SO上找到的唯一類似的問題是嵌套的命名空間路由到了錯誤的控制器,然而,那里沒有接受的答案,而且已經過了一年。 也許另一次嘗試將有助於解決這個問題

您的模塊是API ,但Rails正在尋找Api Ruby的模塊區分大小寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM