gpt4 book ai didi

ruby-on-rails - Rails current_page?与 controller.controller_name 对比

转载 作者:行者123 更新时间:2023-12-03 10:40:13 24 4
gpt4 key购买 nike

我正在实现 current_page?为了测试当前 Controller 和 Action 是否等于某个值,但是在该 Controller / Action 组合上它不会返回 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 routes :
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)

最佳答案

current_page?(root_path)工作正常。

但我不能让它与 :controller 一起工作和 :action
似乎助手需要一个字符串,所以:

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

工作正常。

the doc 的奇怪矛盾.

关于ruby-on-rails - Rails current_page?与 controller.controller_name 对比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5186613/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com