Rails 当前页面?与controller.controller_name

发布于 2024-10-20 02:23:59 字数 988 浏览 7 评论 0原文

我正在视图中实现 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 技术交流群。

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

发布评论

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

评论(2

夏有森光若流苏 2024-10-27 02:23:59

current_page?(root_path) 工作正常。

但我无法让它与 :controller:action 一起使用

看来助手需要一个字符串,所以:

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

也可以正常工作。

文档奇怪的矛盾。

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.

花心好男孩 2024-10-27 02:23:59

当我有两条非常相似的路线时,我遇到了这个问题。看看这个:

match '/galleries/sales' => 'galleries#sales', :as => 'gallery_sales'
match '/galleries/sales/:id' => 'galleries#sales', :as => 'gallery_category_sales'

我的控制器操作根据参数处理输出,我最初这样做是因为我不想重复。

当我这样做时:

current_page?(:controller => 'galleries', :action => 'sales', :id => id)

它在应该返回 true 时没有返回 true,所以我创建了不同的路线和操作,并且效果很好。

I had this issue when I had 2 routes that were very similar. Check this out:

match '/galleries/sales' => 'galleries#sales', :as => 'gallery_sales'
match '/galleries/sales/:id' => 'galleries#sales', :as => 'gallery_category_sales'

My controller action handled the output depending on params, and I originally did this b/c I didn't want duplication.

When I did:

current_page?(:controller => 'galleries', :action => 'sales', :id => id)

It didn't return true when it should have, so I created a different route and action and it worked fine.

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