2012-05-24 84 views
1

我開始首次使用帶有命名空間的路由。我理解以下內容的概念。如何創建自定義導軌命名空間路由

namespace :company do 
    namespace :project, defaults:{format: 'json'} do 
     resources :registration 
    end 
    end 

我的控制器看起來像這樣

class Company::Project::RegistrationController < ApplicationController 

    before_filter :authenticate_user! 
    #responds_to :json 

    def register 

    end 

    def entry 

    end 
end 

所以在資源,我想定義registerentry的路線,我還沒有發現任何東西,真的告訴我該怎麼做。我知道如果我不使用命名空間,那麼我通常會在我的路由文件中做這樣的事情。

match 'company/project/registration/register' => "Registration#register" 

有沒有辦法在命名空間塊內做到這一點?

----------改變後-------------- 在第一個答案中提出下面的建議更改後,這就是運行>耙路線給我

register_company_project_registration POST  /company/project/registration/:id/register(.:format) company/project/Registration#register {:format=>"json"} 
    entry_company_project_registration POST  /company/project/registration/:id/entry(.:format) company/project/Registration#enrty {:format=>"json"} 
    company_project_registration_index GET  /company/project/registration(.:format)    company/project/registration#index {:format=>"json"} 
              POST  /company/project/registration(.:format)    company/project/registration#create {:format=>"json"} 
    new_company_project_registration GET  /company/project/registration/new(.:format)   company/project/registration#new {:format=>"json"} 
    edit_company_project_registration GET  /company/project/registration/:id/edit(.:format)  company/project/registration#edit {:format=>"json"} 
     company_project_registration GET  /company/project/registration/:id(.:format)   company/project/registration#show {:format=>"json"} 
              PUT  /company/project/registration/:id(.:format)   company/project/registration#update {:format=>"json"} 
              DELETE  /company/project/registration/:id(.:format)   company/project/registration#destroy {:format=>"json"} 

回答

4

我希望我能理解你的權利。你想添加額外的路線爲子路線?

做法可能是這樣的:

namespace :company do 
    namespace :project, defaults:{format: 'json'} do 
    resource :registration do 
     member do 
     get 'register', :to => 'registration#register', :as => :register 
     get 'entry', :to => 'registration#enrty', :as => :entry 
     end  
    end 
    end 
end 
+0

是的...這就是我想要的!謝謝。 – mattwallace

+0

如果我運行耙路線我看到公司/項目/註冊#註冊爲路線,但在瀏覽器中,如果我打網址我得到一個路由錯誤,它不存在通過去公司/項目/註冊/註冊 – mattwallace

+0

Plese在這裏複製你的'rake routes'輸出。 – thesis