동일한 리소스를 제공하지만 컨트롤러와 뷰가 다른 두 개의 네임 스페이스로 Rails 애플리케이션을 구성하려고합니다. 다음 cenario 식으로 뭔가 :중첩 된 리소스가있는 네임 스페이스 Rails 3.2.8
rails g controller api/beehives
rails g controller api/beehives/bees
rails g controller api/beehives/honeycombs
내가 컨트롤러있어 폴더 구조 같은 소리 :
routes.rb 비계를 사용
resources :beehives do
resources :bees
resources :honeycombs
end
namespace :api do
resources :beehive, only: [:show] do
resources :bees, only: [:index, :show]
resources :honeycombs, only: [:index, :show]
end
end
, 나는 컨트롤러 구조를 만들었습니다 이 :
컨트롤러/beehives_controller.rb
class Api::BeehivesController < ApplicationController
/beehives_controller.rb
class Api::BeehivesController < ApplicationController
이
컨트롤러/API/벌집/bees_controller.rb
class Api::Beehives::BeesController < ApplicationController
잘
컨트롤러/API, 그것은 쉽다. 이 문맥에서 /beehives/1
은 루트 네임 스페이스 (웹 응용 프로그램 용)로 라우팅하고 /api/beehives/1
은 "api"네임 스페이스 (RESTful 웹 서비스 제공 용)로 라우팅합니다. 그게 정말 잘하고있어. 내가 잘못
uninitialized constant Api::BeesController
을하고 있어요 무엇 : 내가 어떤 이유로, 액세스 /api/beehives/1/bees
하려고 할 때 문제는 네임 스페이스 지정은 중첩 된 리소스에 적용되지 않고 레일이 오류가 침이다?
효과가있었습니다. 감사! – derekstavis
하지만 이봐 요, 이건 내 폴더 구조를 어긴거야. 관련 컨트롤러를'api/beehives' 대신'api' 폴더에 넣어야했습니다. 내부 조직인'api' 폴더에 제공 할 리소스가 두 개 이상 있기 때문에 조직이 파괴됩니다. – derekstavis
@DerekWillianStavis'resources'는'module => 'behives'' 옵션을 허용합니다. 또한 중첩 된 리소스에 대해'scope : module => 'behives'do ... end' 래퍼를 사용할 수 있습니다. – jdoe