Rails current_page?与 controller.controller_name 对比 [英] Rails current_page? versus controller.controller_name
问题描述
我正在实施 current_page?
以测试当前控制器和操作是否等于某个值,但是在该控制器/操作组合上它不会返回 true.
I'm implementing current_page?
in a view to test if the current controller and action is equal to a certain value, however it won't return true when on that controller/action combination.
- if current_page?(:controller => 'pages', :action => 'main')
# doesn't return true when on that controller/action combination
它工作的唯一方法是,如果我使用像这样的更详细的方法:
The only way it is working is if I use a bit more verbose method like so:
- if controller.controller_name == 'pages' && controller.action_name == 'main'
# this works just fine
是我的语法错误还是这里发生了其他事情?有没有更好的方法来做到这一点,比如设置一个 BOOL 或者这是正确的方法?
Is my syntax wrong or is there something else happening here? Is there a better way of doing this, such as setting a BOOL or is this the proper way?
最终目标是仅在主着陆页上显示某个标题,而在所有其他页面上显示不同的标题.
The end goal is to only show a certain header on the main landing page while showing a different header on all other pages.
编辑:来自rake routes
的相关输出:
pages_main GET /pages/main(.:format) {:controller=>"pages", :action=>"main"}
root /(.:format) {:controller=>"pages", :action=>"main"}
此外,这是渲染时的服务器输出:
Also, this is the server output upon rendering:
Started GET "/" for 127.0.0.1 at 2011-03-03 16:54:40 -0500
Processing by PagesController#main as HTML
Rendered pages/main.html.haml within layouts/application (203.8ms)
推荐答案
current_page?(root_path)
工作正常.
但我无法使用 :controller
和 :action
看起来助手需要一个字符串,所以:
It seems the helper expects a string, so:
current_page?(url_for(:controller => 'pages', :action => 'main'))
也很好用.
与文档的奇怪矛盾.
这篇关于Rails current_page?与 controller.controller_name 对比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!