Ruby on rails 如何在`root:to'上包含定位`
返回Ruby on rails 如何在`root:to'上包含定位`,ruby-on-rails,ruby,ruby-on-rails-3,routes,root,Ruby On Rails,Ruby,Ruby On Rails 3,Routes,Root,返回http://localhost:3000/ 但是,我希望始终在根目录中显示完整路径,因为我使用该路径进行布局。我需要我的根永远是: root :to => 'StaticPages#index' 有人知道如何实现它吗 编辑-原因:我正在突出显示当前页面 http://localhost:3000/en/home 链接: 帮手: def当前_p(路径) “当前”如果是当前页面?(路径) 结束 在页面之间导航时,它工作正常。但是,它永远不会突出显示主页,因为没有路径。 有什么想法吗
http://localhost:3000/
但是,我希望始终在根目录中显示完整路径,因为我使用该路径进行布局。我需要我的根永远是:
root :to => 'StaticPages#index'
有人知道如何实现它吗
编辑-原因:我正在突出显示当前页面
http://localhost:3000/en/home
链接:
帮手:
def当前_p(路径)
“当前”如果是当前页面?(路径)
结束
在页面之间导航时,它工作正常。但是,它永远不会突出显示主页,因为没有路径。
有什么想法吗?在您的情况下,我会重写
当前的\u p
帮助程序,并直接重定向到本地化页面,而不是尝试与您的根一起进行黑客攻击,因为每个定义的根总是指根路径/
link:
<%= link_to (t 'nav.home'), home_path, id: current_p(home_path) %>
helper:
def current_p(path)
"current" if current_page?(path)
end
这将把/(某些地区)/主页和/
标记为当前
这样,您就可以完全控制您的实现。感谢您为我指明了正确的方向。他提供的代码解决了这个问题,但是我将在这里注册我已经实现的解决方案
def current_p(path)
paths_to_match = path =~ /\/\w{2}\/home$/ ? ['/', home_path] : [path]
current = nil
paths_to_match.each do |path_to_match|
current = 'current' if current_page?(path_to_match)
end
current
end
不能使用root返回“/”以外的内容,后者是根路径。这就是为什么它被称为根。您总是可以从根目录重定向到另一个适合您的url。如果我可以问一下,为什么您需要用于布局目的的路径?“对我来说这听起来不是个好主意。”比阿特里查兹说。我已经解释了原因。谢天谢地,作为一种选择,你可以试试这样做。不起作用。。。第一次访问页面(http://localhost:3000/
)主页链接无法获取id=“current”
。。。完全没有id。@gabrielhilal抱歉,我有点不确定如何重写助手。编辑后的版本应该可以使用。
def current_p(path)
if path == home_path
'current' if current_page?(path) || current_page?('/')
else
'current' if current_page?(path)
end
end