This project is archived and is in readonly mode.

#4884 ✓invalid
Alex Le

Routing error for restful resource under namespace

Reported by Alex Le | June 17th, 2010 @ 02:47 AM

There seems to be some issues with the routing for resources under a namespace. I have tried this on both Rails 3b3 and 3b4 and both failed:

View code:

<%= form_for @notebook, :url => toggle_app_notebook_path(@notebook), :remote => true do |form| %>
    <%= submit_tag 'Toggle' %>
<% end %>

routes.rb:

namespace :app do

resources :notebooks do
  member do
    get :toggle  # get always works
    post :toggle # Post returns 404
  end
end

end

rake routes generate this:

                                  GET    /app/notebooks/:id/toggle(.:format)                                      {:controller=>"app/notebooks", :action=>"toggle"}
              toggle_app_notebook POST   /app/notebooks/:id/toggle(.:format)                                      {:controller=>"app/notebooks", :action=>"toggle"}

While GET requests work as expected, the POST always failed with a 404 error:

No route matches "/app/notebooks/1/toggle

To get this route to work correctly, I have to declare another matcher outside the resources :notebooks but still under the namespace, and the URL would be recognized correctly:

namespace :app do

match 'notebooks/:id/toggle' => 'notebooks#toggle'
resources :notebooks do
  member do
    get :toggle  
    post :toggle
  end
end

end

I'm on ruby-1.8.7-p249

(edit 1: update formatting)

Comments and changes to this ticket

  • Andrew White

    Andrew White June 17th, 2010 @ 07:07 AM

    It's because the form_for tag is setting the HTTP method to PUT since it thinks it's editing the notebook model instance. Add :html => { :method => :post } to the form_for tag and it works.

  • Alex Le

    Alex Le June 17th, 2010 @ 08:01 AM

    Andrew, thanks a bunch. I have updated my code and your suggestion worked for me. I saw the PUT in the sever log but it didn't ring a bell for me, since the "rake routes" shows that the path is POST so I did not pay too much attention to the generated form code.

    I still think this is a bug, because member methods should be regular POST's, not PUT's. At least this was the way Rails 2.x would work (please correct me if I'm wrong)

  • Andrew White

    Andrew White June 17th, 2010 @ 11:14 AM

    Alex, the form_for code doesn't look at routes when building the form tag - it justs sees an existing record and assumes that you're editing the object which is obviously a HTTP PUT with RESTful routes. You could argue that your toggle method should be a PUT since it's a specific case of editing a record.

    This is exactly the same as in 2.x.

  • Rohit Arondekar

    Rohit Arondekar June 17th, 2010 @ 03:08 PM

    Alex, has Andrew cleared your issue? Can I close this ticket?

  • DHH

    DHH June 17th, 2010 @ 03:35 PM

    • State changed from “new” to “invalid”
  • Alex Le

    Alex Le June 17th, 2010 @ 04:46 PM

    Thanks for clearing up the confusion. Please go ahead and close the issue.

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