gpt4 book ai didi

ruby-on-rails-3 - 如何创建自定义 rails 命名空间路由

转载 作者:行者123 更新时间:2023-12-04 16:52:59 25 4
gpt4 key购买 nike

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

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

我的 Controller 看起来像这样
class Company::Project::RegistrationController  < ApplicationController

before_filter :authenticate_user!
#responds_to :json

def register

end

def entry

end
end

所以在资源中,我想为 register 定义路由和 entry而且我还没有找到任何真正告诉我如何做到这一点的东西。我知道如果我不使用命名空间,那么我通常会在我的路由文件中做这样的事情。
match 'company/project/registration/register' => "Registration#register"

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

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

最佳答案

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

方法可能是这样的:

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

关于ruby-on-rails-3 - 如何创建自定义 rails 命名空间路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10738391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com