【发布时间】:2014-03-13 13:20:40
【问题描述】:
我有一个 Rails 4 应用程序,其中所有控制器和视图都分为两个命名空间,一个面向代理的 backend 和一个面向客户的 frontend:
MyApp::Application.routes.draw do
constraints subdomain: "admin" do
namespace :backend do
resources :events
end
end
constraints subdomain: /.+/ do
namespace :frontend do
resources :events
end
end
end
此外,所有控制器都继承自 BackendController 或 FrontendController:
controllers
├── application_controller.rb
├── backend
│ ├── events_controller.rb
├── backend_controller.rb
├── frontend
│ └── events_controller.rb
├── frontend_controller.rb
现在需要生成路径的所有内容都必须以命名空间为前缀,例如respond_with [:backend, @event] 或form_for [:backend, @event]。
有没有办法为每个控制器的 URL 帮助程序设置默认命名空间,以便从继承自 BackendController 的控制器调用的 respond_with @event 自动假定正确的命名空间?
【问题讨论】:
-
严格来说,默认命名空间是全局命名空间,没有选项可以改变。您可能希望将 Backend 命名空间移动到全局,或设置您自己的助手来支持您想要的行为。但是,与默认值作斗争是很痛苦的。
标签: ruby-on-rails ruby-on-rails-4 rails-routing