Rails、Radiant 和 Regex
我正在开发一个使用 Radiant CMS 的 Rails 网站,并按照中的第一种方法构建有状态导航此链接 。
我正在匹配正则表达式上的 URL,以确定是否显示每个导航链接的活动状态。作为示例,这里有两个示例导航元素,一个用于 Radiant URL /communications/
,另一个用于 /communications/press_releases/
:
<r:if_url matches="/communications\/$/"><li class="bottom-border selected">Communications</li></r:if_url>
<r:unless_url matches="/communications\/$/"><li class="bottom-border"><a href="/communications">Communications</a></li></r:unless_url>
<r:if_url matches="/communications\/press_releases/"><li class="bottom-border selected">Press Releases</li></r:if_url>
<r:unless_url matches="/communications\/press_releases/"><li class="bottom-border"><a href="/communications/press_releases">Press Releases</a></li></r:unless_url>
新闻稿页面一切正常--也就是说,当 URL 为 /communications/press_releases
时,新闻稿导航项将相应地获取“选定”类,并且通信导航项将被取消选择。但是,Communications 正则表达式似乎无法正常运行,因为当 URL 为 /communications/
时,两个元素都不具有“selected”类(因此正则表达式必定无法匹配)。不过,我已经
>> "/communications/".match(/communications\/$/)
=> #<MatchData:0x333a4>
在 IRB 中进行了测试,正如您所看到的,正则表达式似乎工作正常。可能是什么原因造成的?
TL;DR:"/communications/"
与 Ruby shell 中的 /communications\/$/
匹配,但在 Radiant 导航的上下文中不匹配。这是怎么回事?
I'm working on a Rails site that uses the Radiant CMS and am building stateful navigation as per the first method in this link.
I'm matching the URL on regular expressions to determine whether or not to show the active state of each navigation link. As an example, here are two sample navigation elements, one for the Radiant URL /communications/
and one for /communications/press_releases/
:
<r:if_url matches="/communications\/$/"><li class="bottom-border selected">Communications</li></r:if_url>
<r:unless_url matches="/communications\/$/"><li class="bottom-border"><a href="/communications">Communications</a></li></r:unless_url>
<r:if_url matches="/communications\/press_releases/"><li class="bottom-border selected">Press Releases</li></r:if_url>
<r:unless_url matches="/communications\/press_releases/"><li class="bottom-border"><a href="/communications/press_releases">Press Releases</a></li></r:unless_url>
Everything's working fine for the Press Releases page--that is, when the URL is /communications/press_releases
the Press Releases nav item gets the 'selected' class appropriately, and the Communications nav item is unselected. However, the Communications regular expression doesn't seem to be functioning correctly, as when the URL is /communications/
neither element has the 'selected' class (so the regex must be failing to match). However, I've tested
>> "/communications/".match(/communications\/$/)
=> #<MatchData:0x333a4>
in IRB, and as you can see, the regular expression seems to be working fine. What might be causing this?
TL;DR: "/communications/"
matches /communications\/$/
in the Ruby shell but not in the context of the Radiant navigation. What's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从 Radiant 的 wiki 来看,您似乎不需要添加
/
围绕您的正则表达式或转义/
。尝试一下:幕后发生的事情是Radiant 在
matches
中的字符串上调用Regex.new
,因此您之前尝试匹配的正则表达式是这个:它翻译为“斜杠通信斜杠结尾-” line斜线'我真的怀疑这就是你想要的。
Ruby 正则表达式很有趣,因为有开始(
^
)和行结束($
)以及开始(\A
)的符号) 和字符串结尾 (\Z
)。这就是为什么有时您会看到人们在正则表达式中使用\A
和\Z
。From Radiant's wiki, it looks like you don't need to add
/
s around your regexs or escape/
s. Try:What is happening behind the scenes is that Radiant calls
Regex.new
on the string inmatches
, so the regex you were trying to match before was this one:which translates to 'slash communications slash end-of-line slash' which I really doubt is what you want.
Ruby Regexs are interesting in that there are symbols for both start(
^
) and end of line($
) as well as start(\A
) and end of string(\Z
). That's why sometimes you will see people using\A
and\Z
in their regexes.