繁体   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