2014-11-03 4 views
0

Rails-Api (4)를 사용하고 있으며 네임 스페이스가 지정된 라우트 파일에 대해 세 개의 경로 만 필요합니다. 내 routes.rb 파일에서 Rails-Api (4) : Namespaced routes, only : actions

, 나는이 작업을 수행하려고 :

namespace :api do 
    namespace :v1 do 
    resources :documents, only: [:get, :create] 
    resource :system_status, only: [:get]   
    end 
end 

rake routes 저를 가져옵니다 만이 : 오프하여 only:을 경우

  Prefix Verb URI Pattern     Controller#Action 
api_v1_documents POST /api/v1/documents(.:format) api/v1/documents#create 

, 작동 및 제공 나에게 모든 길 (나는 그것을 원하지 않는다).

namespace :api do 
    namespace :v1 do 
    post '/documents',  to: 'documents#create' 
    get '/documents/:id', to: 'documents#show' 
    get '/system_status', to: 'system_status#show'  
    end 
end 

rake routes 나에게이 이상한 결과를 가져옵니다 :

나는이 시도

   Prefix Verb URI Pattern      Controller#Action 
    api_v1_documents POST /api/v1/documents(.:format)  api/v1/documents#create 
       api_v1 GET /api/v1/documents/:id(.:format) api/v1/documents#show 
api_v1_system_status GET /api/v1/system_status(.:format) api/v1/system_status#show 

는 접두사의로 documents#show 나에게 유일한 api_v1을 받고 함께 무슨 일이야 확실하지.

답변

1

HTTP 동사와 해당하는 Rails 작업이 섞여있는 것처럼 보입니다. :get에 대한 자원 경로는 없지만, 두 :index있는 GET 요청에 대한 경로 및 :show

이 대신에 원래의 자원 기반 라우팅을 변경

이 있습니다 :

namespace :api do 
    namespace :v1 do 
    resources :documents, only: [:show, :create] 
    resource :system_status, only: [:show]   
    end 
end 

을 그리고 그 주어야한다은 올바른 경로와 올바른 URL 도우미 접두어를 사용하십시오.

+1

하! 네. 피곤한 두뇌. 감사! –

+0

@MichaelH. 우리 한테 일어난 일. 행운을 빕니다! (또한 가능한 경우 답변을 수락하는 것을 잊지 마십시오) –