Rails 3 route appends _index to route name

前端 未结 3 1226
北荒
北荒 2020-12-05 23:33

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

相关标签:
3条回答
  • 2020-12-05 23:35

    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".

    0 讨论(0)
  • 2020-12-05 23:56

    You should be using either resource :planner either resources :planners depending on what you need. To learn about singular resource and it differences check out Rails Guides.

    0 讨论(0)
  • 2020-12-05 23:58

    Following on from Semyon Perepelitsa's response, note that resource :planner expects the controller's name is PlannersController, whereas resources :planners expects PlannerController.

    If you don't want to rename your controller when changing from resources to resource, you can override the default by specifying resource :planner, controller: :planner.

    0 讨论(0)
提交回复
热议问题