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

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

问题描述

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

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.

相关路线:

Rails 2.3.8:

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 路线:

Rails 3.0 route:

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

rake 路由的输出

Rails 2.3.8

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) 

任何关于为什么添加此 _index 的想法将不胜感激.

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

推荐答案

因为你的资源被命名为 :planner 而不是 :planners,Rails 决定添加_index 到嵌套在下面的任何集合.我猜它是为了可读性.

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天全站免登陆