简体   繁体   English

Rails.application.routes.recognize_path无法识别POST路由

[英]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中搜索代码。

Fixing the problem 解决问题

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.

相关问题 Rails.application.routes.recognize_path,设计经过验证的路由 - Rails.application.routes.recognize_path with devise authenticated route 在rspec测试中调用Rails.application.routes.recognize_path与Rails 3中的任何路由都不匹配 - Calling Rails.application.routes.recognize_path within an rspec test does not match any route in Rails 3 带有未定义方法身份验证的Rails.application.routes.recognize_path错误 - Rails.application.routes.recognize_path error with undefined method authenticate 调用Rails.application.routes.generate()或Rails.application.routes.recognize_path()时,“ RuntimeError:路由集未完成” - 'RuntimeError: route set not finalized' when calling Rails.application.routes.generate() or Rails.application.routes.recognize_path() 在中间件中使用Rails.application.routes.recognize_path会破坏应用程序 - Using Rails.application.routes.recognize_path inside middleware breaks the app 通过recognize_path将请求URL参数与rails路由匹配 - Match request URL params with rails routes via recognize_path 识别rails控制台Session中的路由 - Recognize routes in rails console Session 为什么Rails无法识别路线 - Why Rails dont recognize route Rails测试无法识别路径 - rails testing fails to recognize path 载波文件存储保存在公共目录中,但 rails 无法识别路径 - carrierwave file storage saved in public directory but rails does not recognize path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM