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

1k Views Asked by At

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?

1

There are 1 best solutions below

0
On

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)