Rails 3路线将_index附加到路线名称

问题描述

| 我正在将Rails 2.3.8版本迁移到Rails 3.0,因此我重写了routes文件。当我使用ѭ0list列出路线时,我看到一些路线名称后面附加了
_index
。我不知道为什么。 相关路线: 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路线:
namespace \"tracker\",:path => \"\" do
  resources :planner do
    collection do
      get :step1
      get :add
      get :unsubscribe
      post :unsubscribe
    end
  end
end
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) 
对于为什么要添加此“ 1”的任何想法,将不胜感激。     

解决方法

        因为您的资源命名为
:planner
而不是
:planners
,所以Rails决定将_index添加到嵌套在其下的任何集合中。我的猜测是为了提高可读性。 集合中命名的动作通常会翻译为动词,因此我可以理解为什么这样有意义。以路由文档中给出的典型照片资源示例为例:
resources :photos do
  collection do
    get \'search\'
  end
end

search_photos GET    /photos/search(.:format)
但是,如果相反,我们将资源称为“照片” ...
resources :photo do
  collection do
    get \'search\'
  end
end

search_photo_index GET    /photo/search(.:format)
在第一种情况下,您搜索\“照片\”,在第二种情况下,您搜索\“照片索引\”。     ,        您应根据需要使用
resource :planner
resources :planners
。要了解单一资源及其区别,请查看《 Rails指南》。     ,        继Semyon Perepelitsa的答复之后,请注意,
resource :planner
期望控制器的名称为
PlannersController
,而
resources :planners
期望的控制器名称为
PlannerController
。 如果在从一个资源切换到另一个资源时不想重命名控制器,则可以通过指定
resource :planner,controller: :planner
覆盖默认值。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...