Rails 当前页面?与controller.controller_name
我正在视图中实现 current_page?
来测试当前控制器和操作是否等于某个值,但是在该控制器/操作组合上时它不会返回 true。
- if current_page?(:controller => 'pages', :action => 'main')
# doesn't return true when on that controller/action combination
它起作用的唯一方法是如果我使用更详细的方法,如下所示:
- if controller.controller_name == 'pages' && controller.action_name == 'main'
# this works just fine
我的语法错误还是这里发生了其他事情?有没有更好的方法来做到这一点,例如设置一个 BOOL 或者这是正确的方法?
最终目标是仅在主登陆页面上显示特定标题,而在所有其他页面上显示不同的标题。
编辑:来自rake paths
的相关输出:
pages_main GET /pages/main(.:format) {:controller=>"pages", :action=>"main"}
root /(.:format) {:controller=>"pages", :action=>"main"}
此外,这是渲染时的服务器输出:
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)
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
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.
Edit: Relevant output from 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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
current_page?(root_path)
工作正常。但我无法让它与
:controller
和:action
一起使用看来助手需要一个字符串,所以:
也可以正常工作。
与文档奇怪的矛盾。
current_page?(root_path)
works fine.But I can't make it work with
:controller
and:action
It seems the helper expects a string, so:
works fine too.
Weird contradiction with the doc.
当我有两条非常相似的路线时,我遇到了这个问题。看看这个:
我的控制器操作根据参数处理输出,我最初这样做是因为我不想重复。
当我这样做时:
它在应该返回 true 时没有返回 true,所以我创建了不同的路线和操作,并且效果很好。
I had this issue when I had 2 routes that were very similar. Check this out:
My controller action handled the output depending on params, and I originally did this b/c I didn't want duplication.
When I did:
It didn't return true when it should have, so I created a different route and action and it worked fine.