[英]Rails.application.routes.recognize_path does not recognize POST route
I am using Rails.application.routes.recognize_path
to decompose a path into its components - :controller, :action, :id, etc. It seems to work fine with GET routes, but with POST routes it comes up empty. 我正在使用
Rails.application.routes.recognize_path
将路径分解为其组件--:controller,:action,:id等。它似乎可以正常使用GET路由,但是使用POST路由时它会变空。 Here is the relevant part of my routes file: 这是我的路线文件的相关部分:
resources :units do
member do
get :test_get
post :test_post
end
end
Here is the output from recognize_path for GET: 以下是GET的recogn_path的输出:
Rails.application.routes.recognize_path '/units/1/test_get'
=> {:controller=>"units", :action=>"test_get", :id=>"1"}
Here is the output for POST: 这是POST的输出:
Rails.application.routes.recognize_path '/units/1/test_post'
ActionController::RoutingError: No route matches "/units/1/test_post"
The route is defined - here is the output from rake routes
路线已定义 - 这是
rake routes
的输出
test_get_unit GET /units/:id/test_get(.:format) units#test_get
test_post_unit POST /units/:id/test_post(.:format) units#test_post
What's missing from my path? 我的道路上缺少什么? Is there another method I should be using?
我应该使用另一种方法吗?
Let me amplify this answer. 让我来扩大这个答案。
I ran into exactly the same problem. 我遇到了完全相同的问题。 I had
我有
@controller_action_hash = Rails.application.routes.recognize_path(request.url)
in application_controller.rb. 在application_controller.rb中。 This caused Rails 4 to do a
这导致Rails 4做了
ActionController::RoutingError (No route matches "http://localhost:3000/internal_users/sign_out"):
(I'm using Devise). (我正在使用Devise)。
This caused me to go down a rabbit hole checking routes.rb, running rake routes, and tracing through code in Devise. 这导致我走下兔子洞检查routes.rb,运行rake路线,并在Devise中搜索代码。
In my case the following worked 在我的情况下,以下工作
@controller_action_hash = Rails.application.routes.recognize_path(request.url, method: request.env["REQUEST_METHOD"])
In my case 就我而言
request.env["REQUEST_METHOD"] # => "DELETE"
Found it! 找到了! recognize_path takes a second parameter, a hash of options, one of which is :method.
recogn_path接受第二个参数,一个选项的哈希,其中一个是:方法。 So this works:
这样可行:
Rails.application.routes.recognize_path '/units/1/test_post', method: :post
=> {:controller=>"units", :action=>"test_post", :id=>"1"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.