【问题标题】:How do I create custom rails namespace routes如何创建自定义 Rails 命名空间路由
【发布时间】:2012-05-24 13:23:42
【问题描述】:

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

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"

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

----------修改后-------------- 在下面的第一个答案中做出建议的更改后,这就是运行 >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"}

【问题讨论】:

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


    【解决方案1】:

    我希望我对你的理解是正确的。您想为子路由添加额外的路由吗?

    方法可能是这样的:

    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
    

    【讨论】:

    • 如果我运行 rake 路由,我会看到 company/project/registration#register 作为路由,但是在浏览器中,如果我点击 url,我会收到路由错误,表明它不存在去 company/项目/注册/注册
    • 请在此处复制您的rake routes 输出。
    • 在上面的答案中添加了路线。它们对于 cmets 区域来说太长了。
    • 对不起,我看不到你的rake routes 输出。请编辑您的问题描述并添加您拥有company 基于命名空间的路由(所有基于命名空间的路由)的部分。
    • 好的,应该将运行 rake 路由的路由添加到“问题/问题区域”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-16
    相关资源
    最近更新 更多