This project is archived and is in readonly mode.

#5458 ✓invalid
Jamie Wong

Incorrect routing when controller specified for resources - Rails 3.0.0.rc

Reported by Jamie Wong | August 25th, 2010 @ 05:58 PM

Problem 1

  resources 'account', :controller => 'users', :as => 'users' do
    collection do
      post 'reward_all'
    end

    member do
      post 'reward'
    end
  end

yields the error: "missing :action" - referring the apparent need to specify an action for post 'reward'. This seems redundant, as I would change it to

  post 'reward' => :reward

Problem 2

  resources 'account', :controller => 'users', :as => 'users' do
    collection do
      post 'reward_all'
    end

    member do
      post 'reward' => :reward
    end
  end

yields the following - note the incorrect controller and route name on the reward_all collection route
Also, no default named route is provided for the member action

account_reward_all POST   /account/reward_all         {:controller=>"account", :action=>"reward_all"}
                   POST   /account/:id/reward         {:controller=>"users", :action=>"reward"}
                   GET    /account(.:format)          {:controller=>"users", :action=>"index"}
             users POST   /account(.:format)          {:controller=>"users", :action=>"create"}
          new_user GET    /account/new(.:format)      {:controller=>"users", :action=>"new"}
                   GET    /account/:id(.:format)      {:controller=>"users", :action=>"show"}
                   PUT    /account/:id(.:format)      {:controller=>"users", :action=>"update"}
              user DELETE /account/:id(.:format)      {:controller=>"users", :action=>"destroy"}
         edit_user GET    /account/:id/edit(.:format) {:controller=>"users", :action=>"edit"}

Even stranger, by specifying an action on the collection route, it corrects the controller:

  resources 'account', :controller => 'users', :as => 'users' do
    collection do
      post 'reward_all' => :reward_all
    end

    member do
      post 'reward' => :reward, :as => 'reward'
    end
  end

yields

account_reward_all POST   /account/reward_all         {:controller=>"users", :action=>"reward_all"}
       reward_user POST   /account/:id/reward         {:controller=>"users", :action=>"reward"}
                   GET    /account(.:format)          {:controller=>"users", :action=>"index"}
             users POST   /account(.:format)          {:controller=>"users", :action=>"create"}
          new_user GET    /account/new(.:format)      {:controller=>"users", :action=>"new"}
                   GET    /account/:id(.:format)      {:controller=>"users", :action=>"show"}
                   PUT    /account/:id(.:format)      {:controller=>"users", :action=>"update"}
              user DELETE /account/:id(.:format)      {:controller=>"users", :action=>"destroy"}
         edit_user GET    /account/:id/edit(.:format) {:controller=>"users", :action=>"edit"}

Note that the named route is still wrong, but only for the collection action

Comments and changes to this ticket

  • Jamie Wong

    Jamie Wong August 25th, 2010 @ 06:13 PM

    Also, if I change the post collection line to

        post 'reward_all' => :reward_all, :as => 'reward_all'
    

    It corrects the named route.

    Why are these things not the defaults?

  • Andrew White

    Andrew White August 25th, 2010 @ 09:42 PM

    • State changed from “new” to “invalid”
    • Importance changed from “” to “Low”

    Using your original routes, here's the output from the current master:

    reward_all_users POST   /account/reward_all(.:format) {:action=>"reward_all", :controller=>"users"}
         reward_user POST   /account/:id/reward(.:format) {:action=>"reward", :controller=>"users"}
                     GET    /account(.:format)            {:action=>"index", :controller=>"users"}
               users POST   /account(.:format)            {:action=>"create", :controller=>"users"}
            new_user GET    /account/new(.:format)        {:action=>"new", :controller=>"users"}
           edit_user GET    /account/:id/edit(.:format)   {:action=>"edit", :controller=>"users"}
                     GET    /account/:id(.:format)        {:action=>"show", :controller=>"users"}
                     PUT    /account/:id(.:format)        {:action=>"update", :controller=>"users"}
                user DELETE /account/:id(.:format)        {:action=>"destroy", :controller=>"users"}
    

    There were some differences between symbols and strings which hopefully have now been rectified. If you'd used symbols you'd get the same output in RC2.

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>

Pages