Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Unexpected behaviour in resource mapping

#3179

When :show is excluded from map.resources, the created :destroy route points to the plural of the resource rather than the expected singular (which most url helpers use)

Example:

map.resources :posts, :except => [:show]

Generates the following routes

     posts GET    /posts(.:format)          {:controller=>"posts", :action=>"index"}
           POST   /posts(.:format)          {:controller=>"posts", :action=>"create"}
  new_post GET    /posts/new(.:format)      {:controller=>"posts", :action=>"new"}
  edit_post GET    /posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
           PUT    /posts/:id(.:format)      {:controller=>"posts", :action=>"update"}
           **DELETE /posts/:id(.:format)      {:controller=>"posts", :action=>"destroy"}**

While the expected route by UrlHelpers is

     posts GET    /posts(.:format)          {:controller=>"posts", :action=>"index"}
           POST   /posts(.:format)          {:controller=>"posts", :action=>"create"}
  new_post GET    /posts/new(.:format)      {:controller=>"posts", :action=>"new"}
  edit_post GET    /posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
           PUT    /posts/:id(.:format)      {:controller=>"posts", :action=>"update"}
           **DELETE /post/:id(.:format)      {:controller=>"posts", :action=>"destroy"}**

Reported by cousine · September 9th, 2009 @ 10:18 PM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. cousine
    cousine

    After playing around a little, i found that the ambiguity is not caused by resources

    for example

      map.resources :posts, :except => [:show]
    

    will correctly generate routes (in plural form) as expected

    but if the following routes are drawn

      map.resources :posts, :except => [:show]
      map.post "post/:id", :controller => 'posts', :action => 'show', :conditions => [:method => :get]
    

    Polymorphic routes would try to draw a route for delete based on the named route post, thus it generates "post/:id" for destroy while destroy is mapped with "posts/:id"

    So far as the overriding route is sticking to the plural convention as the Resources route, no problem occurs, but its only irritating if the overriding route is different

    for example this would work fine:

      map.resources :posts, :except => [:show]
      map.post "posts/:id", :controller => 'posts', :action => 'show', :conditions => [:method => :get]
    

    but in this case:

      map.resources :posts, :except => [:show]
      map.post "blog/:id", :controller => 'posts', :action => 'show', :conditions => [:method => :get]
    

    polymorphic_routes would generate a "blog/:id" for the destroy action, while its correct route is "posts/:id", to further show the irritation behind this, i found a solution which involves changing the resources statement to this:

      map.resources :posts, :except => [:show], :as => "blog"
      map.post "blog/:id", :controller => 'posts', :action => 'show', :conditions => [:method => :get]
    

    this will solve the problem, but will change the entire route group to blog instead of posts, and "blog/new" is not equal in meaning as "posts/new".

    September 10th, 2009 @ 12:13 AM

  2. Ryan Bigg
    Ryan Bigg
    • State changed from new to wontfix

    By defining map.post you are overriding post_path/post_url, which is why you were seeing it going to the "wrong" route. Closing.

    June 16th, 2010 @ 11:41 PM