簡體   English   中英

映射到Rails路由關注點的控制器名稱空間中的不一致

[英]Inconsistency in the namespace of controller mapped to Rails' route concern

  concern :votable do
    resources :votes, only: [:index, :create]
  end

  namespace :api, defaults: {format: :json} do
    namespace :v1 do
      namespace :problems do
        resources :details, only: [:index, :show], concerns: :votable

我的routes.rb如上所述,我將VotesController放在api\\v1\\votes_controller.rb 類名是Api::V1::VotesController

它可以在我的本地計算機上正常運行( Windows 7, ruby 2.0.0p481 (2014-05-08) [i386-mingw32], Rails 4.1.5 ),但是在Heroku( ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux], Rails 4.1.5 )。 從日志中,似乎希望VotesController位於api\\v1\\problems\\votes_controller.rb

錯誤日志:

ActionController::RoutingError (uninitialized constant Api::V1::Problems::VotesController):

它是已知的錯誤嗎? 如果我想使votes_controller.rb不在Api :: V1 :: Problems命名空間之內,該如何配置?

我希望它是通用的並且可被任何其他控制器重用,因此避免將其放在api:v1:problems目錄下。

將路由更改為具有scope而不是namespace

namespace :api, defaults: {format: :json} do
    namespace :v1 do
      scope '/problems' do
        resources :details, only: [:index, :show], concerns: :votable

這應該生成這樣的路由:

/api/v1/problems/votes => Api::V1::VotesController

更新:

concern :votable do
  resources :votes, only: [:index, :create]
end

namespace :api, defaults: {format: :json} do
  namespace :v1 do
    scope 'problems' do
      resources :details, only: [:index, :show], concerns: :votable, controller: '/problems/details'
    end
  end
end

路線:

$ rake routes|grep 'api'
api_v1_detail_votes GET    /api/v1/problems/details/:detail_id/votes(.:format) api/v1/votes#index {:format=>:json}
                    POST   /api/v1/problems/details/:detail_id/votes(.:format) api/v1/votes#create {:format=>:json}
     api_v1_details GET    /api/v1/problems/details(.:format)                  api/v1//problems/details#index {:format=>:json}
      api_v1_detail GET    /api/v1/problems/details/:id(.:format)              api/v1//problems/details#show {:format=>:json}

這是您要找的東西嗎?

暫無
暫無

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

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