ruby-on-rails-3 – 嵌套资源子目录中的分组控制器
发布时间:2020-12-16 19:47:16 所属栏目:百科 来源:网络整理
导读:我想组织我的控制器在子目录.这是一个例子: routes.rb中: resources :locations do resources :usersend 我想把我的控制器放在相应的子目录中: app/controllers/locations/users_controller.rb 并且url将是(标准): /locations/1/users/locations/1/users
我想组织我的控制器在子目录.这是一个例子:
routes.rb中: resources :locations do resources :users end 我想把我的控制器放在相应的子目录中: app/controllers/locations/users_controller.rb 并且url将是(标准): /locations/1/users /locations/1/users/new /locations/1/users/10/edit ... 如果我在路由中有一个命名空间,我可以将我的users_controller.rb更改为 class Locations::UsersController < LocationsController end 但它不适用于嵌套资源,而是会收到以下错误: Routing Error uninitialized constant UsersController 更新 如果我补充说: resources :locations do resources :users end match 'locations/:location_id/users' => "locations/users#index" 但是我必须为每个动作和嵌套资源添加路由… 解决方法
如果你想使用那条路线:
match 'locations/:location_id/users' => "locations/users#index" 这可能会在可能与该匹配冲突的任何其他资源/匹配之前发生.默认情况下,Rails路由是顶层的. # should be before locations resource resources :locations do resources :users end 或者,如果您想将所有嵌套的用户资源转移到位置/用户,您可以将控制器分配给资源. resources :locations do resources :users,:controller => "locations/users" end (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- swift – 可选择尝试没有返回值的函数
- 当源文件在从属静态库中更新时,Xcode不会重建应用程序
- iphone – 在storyboard中向scrollview添加项目(大小检查器
- c# – 我们是否有理由在Directory.EnumerateFiles()上使用D
- c# – Type.IsSubclassOf()在AppDomains中不起作用?
- Swift基本语法快速一览
- Wijmo5 Flexgrid基础教程(七)【Angular2创建HTML5的输入控件
- c – 使用for循环删除对象数组
- 为什么字符变得无用? libcurl c Utf-8编码的html;
- Ruby,堆栈级别太深(SystemStackError)