Rails 3.2 视图中基于部分或控制器的类突出显示
我看过这篇文章,并且在HAML 部分。当我访问两个不同的控制器操作时,一个实例正常工作,而另一个实例失败。代码如下:
= link_to_unless_current(t('some.string', :en=>'SomeString'), '/url',{:class=>(controller.controller_name == 'randomController' ? 'current' : 'header-link')})
当我在任一视图中输出 =controller.controller_name
时,我得到“randomController”。在主列表页面(所有对象均分页显示)上,未应用该类,但当移动到“显示”页面时,该类突然出现。由于两者的控制器是相同的(两种情况下都打印相同的字符串),为什么该类没有同等地应用?
一般来说,是否有更好的方法根据当前控制器设置链接样式,而不是检查控制器名称? current_page
帮助器需要控制器和操作,这意味着它不适合这里。
I've looked at this article and am getting strange behavior in a HAML partial. When I access two different controller actions, one instance works while the other fails. Here's the code:
= link_to_unless_current(t('some.string', :en=>'SomeString'), '/url',{:class=>(controller.controller_name == 'randomController' ? 'current' : 'header-link')})
When I output = controller.controller_name
in either view, I get 'randomController.' On the main listing page (where all objects are shown paginated), the class is not applied, but when moving to the 'show' page, the class suddenly appears. As the controller is the same in both (same string is printed in either case), why is it that the class isn't applied equally?
In general, is there a better way to style links based on the current controller, instead of checking the controller name? The current_page
helper requires both controller + action, meaning it's not a fitting candidate here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会将其标记为答案,以防其他人遇到同样的问题。希望这种格式正确:
这显然可以被清理以删除重复的元素,并且最终可能会使用 作为应用程序装饰器draper 以便我可以在网站的任何视图中使用它。
I'm going to mark this as the answer in case others stumble over the same problem. Hopefully this formats properly:
This can be obviously be cleaned up to remove repeated elements, and will probably end up as an application decorator using draper so that I can use it within any view on the site.