This project is archived and is in readonly mode.

#3179 ✓wontfix
cousine

Unexpected behaviour in resource mapping

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

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"}**

Comments and changes to this ticket

  • cousine

    cousine September 10th, 2009 @ 12:13 AM

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

  • Ryan Bigg

    Ryan Bigg June 16th, 2010 @ 11:41 PM

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

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages