Rails 3路线将_index附加到路线名称 [英] Rails 3 route appends _index to route name

查看:95
本文介绍了Rails 3路线将_index附加到路线名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Rails 2.3.8版本迁移到Rails 3.0,因此我重写了routes文件。当我使用 rake路由列出路由时,我看到一些路由名称附加了 _index 。我不知道为什么。



相关路线:



Rails 2.3.8:

  map.namespace tracker,:path_prefix =>  |计划| 
planner.resources:planner,:collection => {:step1 => :get,
:add => :get,
:unsubscribe => [:get,:post]}
结尾

Rails 3.0路线:

 命名空间 tracker,:path => 做
资源:计划者做
收集做
获取:step1
获取:add
获取:取消订阅
发布:取消订阅
结束
结束
结束

耙路的输出



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)

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)

关于w的任何想法添加此 _index 的hy。

解决方案

它是因为您的资源命名为:planner 而不是:planners ,所以Rails决定将_index添加到嵌套在其下的任何集合中。我的猜测是为了提高可读性。



集合中命名的动作通常会转换为动词,因此我可以理解为什么这样有意义。以路由文档中给出的典型照片资源示例为例:

  resources:照片执行
收集执行
获取搜索
结束
结束

search_photos GET /photos/search(.:format)

但是如果相反,我们将资源称为照片 ...

 资源:photo做
收集
做'搜索'
结束
结束

search_photo_index GET /photo/search(.:format)

在第一种情况下,您搜索照片,在第二种情况下,您搜索照片索引。


I am migrating a Rails 2.3.8 version to Rails 3.0 and so ive rewritten my routes file. When i list the routes using rake routes, i see some route names have _index appended to them. I cant figure out why this is.

Relevant routes:

Rails 2.3.8:

map.namespace "tracker", :path_prefix => "" do |planner|
    planner.resources :planner, :collection => {:step1 => :get,
                                                :add => :get,
                                                :unsubscribe => [:get, :post] }
end

Rails 3.0 route:

namespace "tracker", :path => "" do
  resources :planner do
    collection do
      get :step1
      get :add
      get :unsubscribe
      post :unsubscribe
    end
  end
end

Output from 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) 

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) 

Any ideas as to why this _index is being added would be much appreciated.

解决方案

It is because your resource is named :planner instead of :planners that Rails decided to add the _index to any collection nested underneath. My guess it is there for readability.

The action named in the collection normally translates to a verb, so I can see why this makes sense. Take the typical photos resource example given in the routing docs:

resources :photos do
  collection do
    get 'search'
  end
end

search_photos GET    /photos/search(.:format)

But if instead we called the resources 'photo'...

resources :photo do
  collection do
    get 'search'
  end
end

search_photo_index GET    /photo/search(.:format)

In the first case, you search the "photos", and in the second case you search the "photo index".

这篇关于Rails 3路线将_index附加到路线名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆