简体   繁体   中英

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

My routes.rb is as above and I put VotesController in api\\v1\\votes_controller.rb . The class name is Api::V1::VotesController

It run ok on my local machine ( Windows 7, ruby 2.0.0p481 (2014-05-08) [i386-mingw32], Rails 4.1.5 ), however it works differently on Heroku ( ruby 2.0.0p481 (2014-05-08 revision 45883) [x86_64-linux], Rails 4.1.5 ). From the logs, it seems to expect the VotesController to be inside api\\v1\\problems\\votes_controller.rb

Error log:

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

Is it known bug? If I want to make the votes_controller.rb not to be inside Api::V1::Problems namespace, how should I configure it?

I want it to be generic & reusable by any other controllers, so I avoid putting it under api:v1:problems directory.

Change your routes to have scope instead of namespace :

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

This should generate route like this:

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

UPDATE:

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

routes:

$ 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}

Is this what you're looking for?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM