如果referer =“/search",则执行X,否则执行Y:“错误参数(预期的URI对象或URI字符串)"; [英] If referer = "/search", do X, else do Y: "bad argument (expected URI object or URI string)"
问题描述
我在使用 Ruby 和 URI 时遇到问题.我想测试我的推荐人:
I have a problem with Ruby and URI. I want to test my referer:
- 如果引用包含/search",则显示X
- else 显示 Y
控制器:
@referer = URI(request.referer).path
查看:
<% if (@referer == '/search') %>
<p><%= link_to('<< Back to results', :back)%></p>
<% else %>
<p>Tadamm</p>
<% end %>
问题:当有引用时,例如搜索",就可以了.当没有引用者时,比如直接访问,我有这个错误:
Problem: When there's a referer, like "search", it's ok. When there's no referer, like direct access, I have this error:
错误参数(预期的 URI 对象或 URI 字符串)
bad argument (expected URI object or URI string)
你能帮我吗?
推荐答案
@referer = URI(request.referer).path
URI
方法需要URI 对象或 URI 字符串".但是如果没有推荐人,那么你就不会给它任何一个.这相当于调用 URI(nil).path
.一个快速的解决方法是:
The URI
method expects a "URI object or URI string." But if there's no referer, then you're not giving it either of those. This is equivalent to calling URI(nil).path
. A quick fix would be:
@referer = request.referer && URI(request.referer).path
这样,如果没有引用者,@referer
将设置为 nil
,并且您的 if
语句将按预期工作.
This way, if there's no referer, @referer
will be set to nil
, and your if
statement will work as expected.
这篇关于如果referer =“/search",则执行X,否则执行Y:“错误参数(预期的URI对象或URI字符串)";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!