ruby on Rails 和部分动态菜单

发布于 2024-12-02 23:02:08 字数 2149 浏览 1 评论 0原文

我对 Rails 还很陌生,希望获得一些帮助来使用部分实现动态菜单。

截至目前,我的代码如下:

菜单部分:

.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

视图:

  html...
  = render :partial => 'shared/aboutmenu_nav'
  more html...

页面助手:

def @active_page(c) 
controller.action_name == c 
end

注意:active_page是我想传递给li 如果用户位于相应的页面上。例如,如果用户位于“联系人”页面上,则 active_page 类将仅传递给“联系人 li”元素。

我尝试了几种解决方案,包括创建多个 CSS 版本并在与页面对应的 body 上传递一个类?例如,

%body{:class => @page_name}

然后在pages_helper中定义@page_name,如下所示:

 def page_name
    @path.parameterize('_') unless @path.blank?
  end

这两个版本似乎都不起作用,因为前一个(@active_page)不会产生任何结果。同样,我无法在 body 标签上分配类——当我检查源代码时,body 标签没有任何类。

对于如何使用部分实现动态菜单的任何帮助,我将不胜感激。如果有我没有尝试过的方法,请随时告诉我。我衷心感谢您的所有帮助,也非常感谢您的宝贵时间!谢谢你!

使用 Kanadada 的解决方案。该页面在样式化菜单应出现的位置逐字显示以下文本:

<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>

如果该代码位于源代码中,那就太好了;但是,源代码似乎无法识别它...相反,源代码显示以下代码:

 &lt;li class=&quot;&quot;&gt;&lt;a href=&quot;/pages/about&quot;&gt;About&lt;/a&gt;&lt;/li&gt;&lt;li class=&quot;active_page&quot;&gt;&lt;a href=&quot;/pages/careers&quot;&gt;Careers&lt;/a&gt;&lt;/li&gt;&lt;li class=&quot;&quot;&gt;&lt;a href=&quot;/pages/contact&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;

我怀疑它可能与 HAML 中解释 =top_menu 的方式有关?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黑凤梨 2024-12-09 23:02:08

将这些方法添加到 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

现在在您的视图中:

.three.columns
  %ul#nav
    = top_menu

注意:

使用路由助手作为路径,而不是对其进行硬编码。假设您有一个名为 PagesController 的控制器,您可以重写 top_menu 方法,如下所示:

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

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