Selvamani
Selvamani

Reputation: 7684

get last visited url using ruby on rails

I visit localhost from my normal html file. The html code is

<!DOCTYPE html>
<html>
<body>
<div id="deal">
<a href="http://localhost:3000">Home</a>
</div>
</body>
</html>

my file path is

file:///home/selva/Desktop/test.html#deal

When I click Home link, I need to get #deal in to rails project. So I use this method URI(request.referer).path

require 'uri'
class RefUrlAuthentication < ::Devise::Strategies::Authenticatable
  def valid? 
    puts "PARAMS: #{URI(request.referer).path}"
  end
end

And I have tried request.referer but no luck.

Here I am getting this error

ArgumentError (bad argument (expected URI object or URI string)):
lib/custom_authentication.rb:100:in valid?' 

this error occur when I use URI(request.referer).path

I can't figure out what's wrong in this code. How to I ignore this error and get the url file url. thanks for you time.

Upvotes: 1

Views: 1673

Answers (1)

Patrick Oscity
Patrick Oscity

Reputation: 54684

request.referer can be nil, specifically because there is no referer when the user navigates to the site directly by typing in the url manually. You can however default the referer to the empty string:

URI(request.referer || '').path

Side note: The referer is determined from the HTTP header HTTP_REFERER, which is set by the browser. This means that you cannot rely on the data to be correct or even present. Whatever you are trying to accomplish in your authentication strategy, do not base the decision whether a user is authenticated or not on the referer. Someone might be tampering with the value.

Upvotes: 1

Related Questions