简体   繁体   English

Rails 3.0.1中的奇怪路由错误

[英]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: 我在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: 当我运行rake routes ,preview_revision路由正确显示:

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: 但是,当我向/ ads / 145 / preview_revision发出POST请求时,出现以下错误:

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. id(145)存在,是的。
  • The action and the controller (action: preview_revision, controller: ads) exist as well. 操作和控制器(操作:preview_revision,controller:ads)也存在。
  • 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. 我仔细检查了它实际上是一个POST请求,ID是正确的,我真的不知道该怎么办了。
  • Rails version is 3.0.1 Rails版本是3.0.1

--- UPDATE --- ---更新---

I tried changing the method from POST to PUT, and now it works. 我尝试将方法从POST改为PUT,现在它可以工作了。 Apparently POST routes on member level are not working, or not allowed. 显然,成员级别的POST路由不起作用,或者不允许。

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? 我知道POST是用于创建成员的,所以对成员的POST请求没有任何意义,但我没有在Rails指南中的任何地方找到这个,为什么rake routes显示路由,如果不是实际上工作? Is that a bug? 那是一个错误吗?

--- UPDATE 2 --- ---更新2 ---

The Rails Routing guide (Edge version) specifically says: Rails路由指南(Edge版)具体说:

2.9.1 Adding Member Routes 2.9.1添加成员路由

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. 这将通过GET识别/ photos / 1 / preview,并路由到PhotosController的预览操作。 It will also create the preview_photo_url and preview_photo_path helpers. 它还将创建preview_photo_url和preview_photo_path助手。

Within the block of member routes, each route name specifies the HTTP verb that it will recognize. 在成员路由块中,每个路由名称指定它将识别的HTTP谓词。 You can use get, put, post , or delete here. 您可以在此处使用get,put, post或delete。 If you don't have multiple member routes, you can also pass :on to a route, eliminating the block [...] 如果你没有多个成员路由,你也可以传递:on到一个路由,消除阻塞[...]

So is the POST on member possible or not possible? 成员的POST可能或不可能吗? 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: 如果您在积极维护的开源软件中发现错误,最好的办法是:

  • 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. 如果可以的话,包括修复bug的补丁。

The bug tracker for Rails is at rails.lighthouseapp.com . Rails的bug跟踪器位于rails.lighthouseapp.com

---edit--- - -编辑 - -

The bug tracker for Rails is now at github.com/rails/rails/issues . Rails的bug跟踪器现在位于github.com/rails/rails/issues

That should be working. 这应该是有效的。 I've created a blank app with 3.0.1 and added those routes and it worked. 我用3.0.1创建了一个空白的应用程序并添加了这些路线并且它有效。

Have you tried passing the :on param? 你试过传递:on param吗? Something like: 就像是:

resources :ads do
  post :preview_revision, :on => :member
  get :revise, :on => :member

  get :search, :on => :collection
  post :preview, :on => :collection
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM