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

因此,在资源中,我想为registerand定义路线,entry但我没有找到任何真正告诉我如何做到这一点的东西。我知道如果我不使用命名空间,那么我通常会在我的路由文件中执行类似的操作。

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

有没有办法在命名空间块中做到这一点?

---------- 更改后 -------------- 在第一个答案中进行以下建议更改后,这就是运行 >rake routes 给我的

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

1 回答 1

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
于 2012-05-24T13:34:01.847 回答