简体   繁体   中英

Rails.application.routes.recognize_path error with undefined method authenticate

In my application_controller.rb I have a line

# Get the previous url string nicely.
previousPath = Rails.application.routes.recognize_path(request.referrer)

But I get this error on that line

NoMethodError (undefined method `authenticate?' for nil:NilClass)

Which seems like something to do with devise. But when I do include Devise::TestHelpers, I get env is not defined or something, which doesn't seem like a good solution in any way possible.

Any ideas on how to solve this without needing me to catch errors or regexp to split the path?

This issue may be due to route path which is written in routes by default as and when the application loads first, so join the string to the url you get from request.referrer.

ie, url = request.referrer + "controller_name"

 previousPath = Rails.application.routes.recognize_path(url)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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