Rails、Radiant 和 Regex

发布于 2024-09-27 04:03:23 字数 1628 浏览 5 评论 0原文

我正在开发一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-10-04 04:03:23

Radiant 的 wiki 来看,您似乎不需要添加 / 围绕您的正则表达式或转义 /。尝试一下:

<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>

幕后发生的事情是Radiant 在 matches 中的字符串上调用 Regex.new,因此您之前尝试匹配的正则表达式是这个:

 Regexp.new '/communications\/$/'
# => /\/communications\/$\// 

它翻译为“斜杠通信斜杠结尾-” 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:

<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>

What is happening behind the scenes is that Radiant calls Regex.new on the string in matches, so the regex you were trying to match before was this one:

 Regexp.new '/communications\/$/'
# => /\/communications\/$\// 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文