zsq*_*are 30 routes ruby-on-rails ruby-on-rails-3
我正在将Rails 2.3.8版本迁移到Rails 3.0,因此我重写了我的路由文件.当我列出使用的路线时rake routes,我看到一些路线名称已_index附加到它们.我无法弄清楚为什么会这样.
相关路线:
Rails 2.3.8:
map.namespace "tracker", :path_prefix => "" do |planner|
planner.resources :planner, :collection => {:step1 => :get,
:add => :get,
:unsubscribe => [:get, :post] }
end
Run Code Online (Sandbox Code Playgroud)
Rails 3.0路由:
namespace "tracker", :path => "" do
resources :planner do
collection do
get :step1
get :add
get :unsubscribe
post :unsubscribe
end
end
end
Run Code Online (Sandbox Code Playgroud)
输出来自 rake routes
Rails 2.3.8
step1_tracker_planner GET /planner/step1(.:format)
add_tracker_planner GET /planner/add(.:format)
unsubscribe_tracker_planner GET /planner/unsubscribe(.:format)
POST /planner/unsubscribe(.:format)
Run Code Online (Sandbox Code Playgroud)
Rails 3.0
step1_tracker_planner_index GET /planner/step1(.:format)
add_tracker_planner_index GET /planner/add(.:format)
unsubscribe_tracker_planner_index GET /planner/unsubscribe(.:format)
POST /planner/unsubscribe(.:format)
Run Code Online (Sandbox Code Playgroud)
关于为什么_index添加这个的任何想法都将非常感激.
Aar*_*nni 47
这是因为您的资源被命名:planner而不是:plannersRails决定将_index添加到嵌套在下面的任何集合.我的猜测是可读性.
集合中命名的动作通常转换为动词,所以我可以看出为什么这是有意义的.以路线文档中给出的典型照片资源示例为例:
resources :photos do
collection do
get 'search'
end
end
search_photos GET /photos/search(.:format)
Run Code Online (Sandbox Code Playgroud)
但如果相反我们称资源'照片'......
resources :photo do
collection do
get 'search'
end
end
search_photo_index GET /photo/search(.:format)
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,您搜索"照片",在第二种情况下,您搜索"照片索引".
| 归档时间: |
|
| 查看次数: |
5908 次 |
| 最近记录: |