动态菜单ruby在rails和部分 [英] Dynamic menu ruby on rails and partial
问题描述
到目前为止,我的代码如下:
/ p>
菜单部分:
.three.columns
%ul#nav
%li {:class => @active_page ==about? active_page:}
%a {:href => ../pages/about}关于
%li {:class => activeers? active_page:}
%a {:href => ../pages/careers} Careers
%li {:class => @active_page ==contact? active_page:}
%a {:href => ../pages/contact}联系人
视图:
html ...
= render:partial => 'shared / aboutmenu_nav'
more html ...
页面助手:
def @active_page(c)
controller.action_name == c
end
注意: active_page
是我想传递给 li
如果用户在相应的页面上。例如,如果用户在联系人页面上,active_page类将只传递给Contact li元素。
我已经尝试过几个解决方案,包括创建多个CSS版本并在与页面相对应的正文
上传递一个类?例如
%body {:class => @page_name}
,然后在pages_helper中定义@page_name,如下所示:
def page_name
@ path.parameterize('_')unless @ path.blank?
end
这两个版本似乎都不起作用,因为前者(@active_page)不产生任何结果。类似地,我无法在body标签上分配一个类 - 当我检查源代码时,body标签没有任何类。
任何帮助如何使用partials实现动态菜单。如果有一个方法我没有尝试,请随时通知我。我真诚地感谢你的所有帮助,我非常感谢你的时间!谢谢!
使用Kanadada的解决方案。该页面字面上显示以下文本,其中应该出现样式菜单:
< li class =
< a href =/ pages / about>关于< / a>
< / li>
< li class =active_page>
< a href =/ pages / careers>职业< / a>
< / li>
< li class =>
< a href =/ pages / contact>联系人< / a>
< / li>
如果代码在源代码中,然而,源代码似乎不认识它...而是源代码显示以下代码:
& lt; li class =& quot;& quot;& gt;& lt; a href =& quot; / pages / about& gt;关于& lt; / a& gt;& / li& gt;& lt; li class =& quot; active_page& quot;& gt;& lt; a href =& quot; / pages / careers& gt; Careers& / a& gt;& lt; / li& gt;& lt; li class =& quot;& quot;& gt;& lt; a href =& quot; / pages / contact& ;& gt; Contact& lt; / a& gt;& lt; / li& gt;
我怀疑它可能与 = top_menu
在HAML中解释
将这些方法添加到 helpers / appalication_helper。 rb
def active_menu?(path,path_right = request.path)
:: parse(path).path == path_right)rescue false
end
def top_menu
[
About,../pages/about ,
Careers,../pages/careers,
Contact,../pages/contact
] .each_slice(2).map do | name,路径|
content_tag(:li,link_to(name,path),
:class =>(active_menu?(path)?active_page:))
end.join ).html_safe
end
现在在您的视图中:
.three.columns
%ul#nav
= top_menu
注意:
使用路径帮助器来代替路径,假设你有一个控制器 PagesController
,你可以重写 top_menu
方法如下:
def top_menu
[
关于,about_pages_path,
职业,careers_pages_path,
联系,contact_pages_path
] .each_slice(2).map do | name,path |
content_tag(:li,link_to(name,path),
:class =>(active_menu?(path)?active_page:))
end.join ).html_safe
end
i'm pretty new to rails and would like some help with implementing a dynamic menu using partials.
As of right now, my code is as follows:
the menu partial:
.three.columns
%ul#nav
%li{:class => @active_page=="about" ? "active_page" : ""}
%a{:href => "../pages/about"} About
%li{:class => @active_page=="careers" ? "active_page" : ""}
%a{:href => "../pages/careers"} Careers
%li{:class => @active_page=="contact" ? "active_page" : ""}
%a{:href => "../pages/contact"} Contact
the view:
html...
= render :partial => 'shared/aboutmenu_nav'
more html...
the pages helper:
def @active_page(c)
controller.action_name == c
end
Note: active_page
is the class that I would like to pass to the li
if the user is on the corresponding page. For example, if the user is on the Contact page, the active_page class would be passed to only the Contact li element.
I've tried several solutions including creating multiple CSS versions and passing a class on the body
corresponding with the page? for example,
%body{:class => @page_name}
and then defining @page_name in the pages_helper as follows:
def page_name
@path.parameterize('_') unless @path.blank?
end
Neither of these versions seem to work, as the former (@active_page) does not yield any results. Similarly, I was unable to assign a class on the body tag--when I checked the source code, the body tag did not have any classes.
I'd appreciate any help on how to implement a dynamic menu using partials. If there is a method I have not tried please feel free to inform me. I sincerely appreciate all of your help, and I'm very thankful for your time! Thank you!
Using Kanadada's solution. The page literally displays the following text where the styled menu should appear:
<li class="">
<a href="/pages/about">About</a>
</li>
<li class="active_page">
<a href="/pages/careers">Careers</a>
</li>
<li class="">
<a href="/pages/contact">Contact</a>
</li>
This code would be great if it was in the source code; however, the source does not seem to recognize it... instead the source code shows the following code:
<li class=""><a href="/pages/about">About</a></li><li class="active_page"><a href="/pages/careers">Careers</a></li><li class=""><a href="/pages/contact">Contact</a></li>
I suspect that it might have something to do with the way =top_menu
is interpreted in HAML?
Add these methods to helpers/appalication_helper.rb
def active_menu?(path, path_right=request.path)
(URI::parse(path).path == path_right) rescue false
end
def top_menu
[
"About", "../pages/about",
"Careers", "../pages/careers",
"Contact", "../pages/contact"
].each_slice(2).map do |name, path|
content_tag(:li, link_to(name, path),
:class => (active_menu?(path) ? "active_page" : ""))
end.join('').html_safe
end
Now in your view:
.three.columns
%ul#nav
= top_menu
Note:
Use route helpers for path instead of hard coding them. Assuming you have a controller called PagesController
, you can rewrite the top_menu
method as follows:
def top_menu
[
"About", about_pages_path,
"Careers", careers_pages_path,
"Contact", contact_pages_path
].each_slice(2).map do |name, path|
content_tag(:li, link_to(name, path),
:class => (active_menu?(path) ? "active_page" : ""))
end.join('').html_safe
end
这篇关于动态菜单ruby在rails和部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!