This irritates me right now, and I've been at it for more than an hour. Maybe one of you has an idea.
I've defined the following routes in routes.rb:
resources :ads do
member do
post :preview_revision
get :revise
end
collection do
get :search
post :preview
end
end
When I run rake routes
, the preview_revision route is shown correctly:
preview_revision_ad POST /ads/:id/preview_revision(.:format) {:action=>"preview_revision", :controller=>"ads"}
However, when I make a POST request to /ads/145/preview_revision, I get the following error:
Started POST "/ads/145/preview_revision" for 127.0.0.1 at Mon Nov 15 17:45:51 +0100 2010
ActionController::RoutingError (No route matches "/ads/145/preview_revision"):
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.1/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.2ms)
--- UPDATE ---
I tried changing the method from POST to PUT, and now it works. Apparently POST routes on member level are not working, or not allowed.
Still, I want to know why. I do know that POST is for creating a member, so a POST request on a member makes no sense, but I didn't find this mentioned anywhere in the Rails guides, and why would rake routes
display the route, if it doesn't actually work? Is that a bug?
--- UPDATE 2 ---
The Rails Routing guide (Edge version) specifically says:
2.9.1 Adding Member Routes
To add a member route, just add a member block into the resource block:
resources :photos do member do get 'preview' end end
This will recognize /photos/1/preview with GET, and route to the preview action of PhotosController. It will also create the preview_photo_url and preview_photo_path helpers.
Within the block of member routes, each route name specifies the HTTP verb that it will recognize. You can use get, put, post, or delete here. If you don’t have multiple member routes, you can also pass :on to a route, eliminating the block [...]
So is the POST on member possible or not possible? Am I missing something? Just to be clear, I don't wanna use it anymore, but still, it bugs me not knowing why it doesn't work.
The best thing to do, if you find a bug in actively maintained open-source software, is:
The bug tracker for Rails is at rails.lighthouseapp.com.
---edit---
The bug tracker for Rails is now at github.com/rails/rails/issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With