在直接访问某个网页时,忽略上一个网址的URI(request.referer).path [英] Ignore URI(request.referer).path for previous URL when visiting directly to a page
问题描述
我尝试在会话中保存以前的网址,我目前有以下代码:
after_filtersave_my_previous_url ,only:[:renderForm,:renderQuote]
def save_my_previous_url
如果URI(request.referer).path
session [:my_previouse_url] = URI(request.referer)。 path
else
session [:my_previouse_url] = URI(request.referer).path
end
end
当你从一个页面,到表单,然后到谢谢页面,它工作,但如果你直接直接而不是单击一个链接,我得到此错误:
错误的参数(期望的URI对象或URI字符串)
pre>
所以我需要一些在没有以前的URL时忽略这个。
解决方案您需要确保处理nil引荐来源网址和引荐来源网址无法解析的网址(例如网址为 http://x.com?utm_term=a|b - |会导致解析失败,至少在rub的某些版本中)。
此代码(或类似代码)适用于我:
begin
if request.referer.present? &&& URI.parse(request.referer).path
session [:my_previouse_url] = URI(request.referer).path
else
session [:my_previouse_url] = nil;
end
rescue URI :: Error => e
session [:my_previouse_url] = nil;
end
I'm trying to save the previous URL in a session, I currently have this code:
after_filter "save_my_previous_url", only: [:renderForm, :renderQuote] def save_my_previous_url if URI(request.referer).path session[:my_previouse_url] = URI(request.referer).path else session[:my_previouse_url] = URI(request.referer).path end end
It works when you go from a page, to the form, then to the thank you page, but if you go straight to a form directly instead of clicking a link, I get this error:
bad argument (expected URI object or URI string)
So I need to some how ignore this when there isn't a previous url.
解决方案You need to make sure that you handle both nil referrer and referrer's who URLs can't parse (e.g. urls like http://x.com?utm_term=a|b -- "|" causes the parsing to fail, at least in some versions of rub).
This code (or something like it) works for me:
begin if request.referer.present? && URI.parse(request.referer).path session[:my_previouse_url] = URI(request.referer).path else session[:my_previouse_url]=nil; end rescue URI::Error => e session[:my_previouse_url]=nil; end
这篇关于在直接访问某个网页时,忽略上一个网址的URI(request.referer).path的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!