Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange routing error in Rails 3.0.1

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)
  • The id (145) exists, yes.
  • The action and the controller (action: preview_revision, controller: ads) exist as well.
  • All the other routes work perfectly fine
  • I've tried restarting the server several times
  • I double-checked that it's actually a POST request, that the ID is correct, I really don't know what to do anymore.
  • Rails version is 3.0.1

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

like image 835
M. Cypher Avatar asked Nov 15 '22 05:11

M. Cypher


1 Answers

The best thing to do, if you find a bug in actively maintained open-source software, is:

  • to submit a bug report to the project's bug tracker isolating the problem as much as possible and with as much detail as possible, and, optionally,
  • to include a patch that fixes the bug if you can.

The bug tracker for Rails is at rails.lighthouseapp.com.

---edit---

The bug tracker for Rails is now at github.com/rails/rails/issues.

like image 107
yfeldblum Avatar answered Dec 19 '22 17:12

yfeldblum