在rails上使用ruby获取最后访问的url

时间:2014-03-12 11:41:44

标签: ruby-on-rails ruby uri

我从普通的html文件访问localhost。 HTML代码是

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

我的文件路径是

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

当我点击Home链接时,我需要将#deal输入到rails项目中。所以我使用这种方法URI(request.referer).path

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

我尝试request.referer但没有运气。

我收到此错误

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

当我使用URI(request.referer).path

时会发生此错误

我无法弄清楚此代码中的错误。如何忽略此错误并获取url文件URL。谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

request.referer可以是nil,特别是因为当用户通过手动输入网址直接导航到网站时没有引用。但是,您可以将引用者默认为空字符串:

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

附注:引用是根据浏览器设置的HTTP标头HTTP_REFERER确定的。这意味着您不能依赖数据是正确的,甚至不存在。无论您在身份验证策略中尝试完成什么,都不要根据引用者是否对用户进行身份验证做出决策。有人可能会篡改价值。