I reorganized the rails application in such a way that for each auxiliary resource I create a controller - the corresponding namespace.
api/v1/app/controller/manager.rb
api/v1/app/controller/manager/user.rb
api/v1/app/controller/manager/controller.rb
api/v1/app/controller/admin.rb
api/v1/app/controller/user.rb
api/v1/app/controller/controller.rb
The definition of the user resource class in the manager namespace is as follows:
class Api::V1::Manager::UserController < ApplicationController
This controller is accessible via route.rb:
resources :manager , only: [:show ] do
resources :user, only: [:index], controller: 'manager/user'
end
which generates
/api/v1/manager/:manager_id/user(.:format) api/v1/manager/manager
All models are under
app/models/manager.rb
app/models/user.rb
When I want to access the model Managerinside the controller api/v1/app/controller/manager/user.rbor in api/v1/app/controller/manager.rbfor example
class Api::V1::ManagerController < ApplicationController
def index
Manager.find(...)
end
end
class Api::V1::Manager::UserController < ApplicationController
def index
Manager.find(...)
end
end
I get these errors
{"error":"uninitialized constant Api::V1::Manager::UserController::Manager"}%
{"error":"uninitialized constant Api::V1::Manager::Manager"}%
Calls are handled by the correct controllers:
Processing by Api::V1::Manager::UserController#index as JSON
The solution is to use a double colon prefix with a call
`::Manager.find(...)`.
I can use all other models Admin.find(...)or Controller.firstnormally. Just Manager.find(..)not working.
ManagerResource .
- , ?
api/v1/app/controller/api/v1/foo/customer_controller.rb
api/v1/app/controller/api/v1/manager_customer_controller.rb
(webrick) .
Manager.first - , Manager...,
`uninitialized constant Api::V1::Foo::UserController::Manager`
`uninitialized constant Api::V1::ManagerUserController::Manager`
`uninitialized constant Api::V1::*any_controller*::Manager`
.
Controller.first , . api/v1/app/controller/controller.rb. .
@Andrey Deineko , , .
, , , ?
II
, . .
Manger. Manager.class Class.
:
module Api
module V1
class Manager < Api::ApiBaseController
def index
puts User.class
puts Manager.class
puts ::Manager.class
puts Controller.class
...
end
end
end
end
class Api::V1::Manager < Api::ApiBaseController
def index
puts User.class
puts Manager.class
puts ::Manager.class
puts Controller.class
...
end
end
, ::Manager , , , ,
class Api::V1::Manager < Api::ApiBaseController
def index
puts User.class
::puts Manager.class
puts Manager.class
puts Controller.class
...
end
end
Api::V1::... .