如何创建自定义rails命名空间路由

时间:2012-05-24 13:23:42

标签: ruby-on-rails-3 namespaces routes

我第一次开始使用带名称空间的路由。我理解以下内容的概念。

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"

有没有办法在命名空间块中执行此操作?

----------变更后-------------- 在第一个答案中进行以下建议更改后,这就是运行&gt; rake路由给我的

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"}

1 个答案:

答案 0 :(得分: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
相关问题