Rails current_page?与controller.controller_name [英] Rails current_page? versus controller.controller_name

查看:312
本文介绍了Rails current_page?与controller.controller_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个视图中实现 current_page?,以测试当前控制器和操作是否等于某个值,但它不会返回true控制器/操作组合。

   - 如果current_page?(:controller =>'pages',:action =>'main ')
#在控制器/操作组合上不返回true

方法它是工作是如果我使用一个更详细的方法像这样:

   - 如果controller.controller_name ==' && controller.action_name =='main'
#这个工作很好

或者还有别的事情发生在这里?有没有更好的方法这样做,如设置BOOL或这是正确的方式?



最终目标是只显示一个特定的标题在主登陆页面,而在所有其他页面上显示不同的标题。



编辑 rake路线的相关输出

  pages_main GET /pages/main(.:format){:controller =>pages,:action =>main} 

root /(.:format){:controller =>pages,:action =>main}

此外,这是渲染时的服务器输出:

  127.0.0.1 at 2011-03-03 16:54:40 -0500 
由PagesController处理#main作为HTML
在布局/应用程序(203.6ms)内渲染的页面/ main.html.haml


解决方案

current_page?(root_path)工作正常。



但我不能使用:controller :action



看起来帮助者需要一个字符串,因此:

  current_page?(url_for(:controller =>'pages',:action =>'main'))


文档


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)

解决方案

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:

current_page?(url_for(:controller => 'pages', :action => 'main')) 

works fine too.

Weird contradiction with the doc.

这篇关于Rails current_page?与controller.controller_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆