如何在 Rails 3 中创建动态生成的面包屑? [英] How do I create a dynamically generated breadcrumb in Rails 3?

查看:38
本文介绍了如何在 Rails 3 中创建动态生成的面包屑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

面包屑是一种导航元素,用于告诉用户他们在网站上的位置.

例如

主页 >> 项目 >> 阶段 >> 上传.

家、项目、舞台和地方上传是单独的控制器.

解决方案

我使用几乎相同的代码将近 10 年......首先用 ASP 编写,然后是 C#、PHP,现在是 Rails:

模块 NavigationHelperdef ensure_navigation@navigation ||= [ { :title =>'主页', :url =>'/' } ]结尾def navigation_add(标题,网址)确保导航<<{:标题=>标题, :url =>网址 }结尾def render_navigation渲染:部分=>'共享/导航', :locals =>{:导航=>确保导航 }结尾结尾

然后,在shared/navigation.html.erb:

<% nav.each do |n|%><%= link_to n[:title], n[:url] %><%结束%><%= link_to yield(:title), request.path, :class =>'无链接' %>

您的常规视图:

<% content_for :title, '我的页面标题' %><% navigation_add '我的父页面标题', parent_page_path %>

还有你的模板:

<头><标题><%=产率:标题%><身体>...<%=render_navigation %>...<%=产率%></html>

Breadcrumb being the navigational element that is used to tell the user where they are on the site.

E.g.

Home >> Projects >> Stages >> Uploads.

Where home, projects, stages & uploads are separate controllers.

解决方案

I use almost the same code for like 10 years... Wrote it first in ASP, then C#, PHP, and now Rails:

module NavigationHelper
    def ensure_navigation
        @navigation ||= [ { :title => 'Home', :url => '/' } ]
    end

    def navigation_add(title, url)
        ensure_navigation << { :title => title, :url => url }
    end

    def render_navigation
        render :partial => 'shared/navigation', :locals => { :nav => ensure_navigation }
    end
end

Then, in shared/navigation.html.erb:

<div class="history-nav">
    <% nav.each do |n| %>
        <%= link_to n[:title], n[:url] %>
    <% end %>
    <%= link_to yield(:title), request.path, :class => 'no-link' %>
</div>

Your regular view:

<% content_for :title, 'My Page Title' %>
<% navigation_add 'My Parent Page Title', parent_page_path %>

And your template:

<html>
<head>
  <title> <%= yield :title %> </title>
</head>
<body>
  ...
  <%= render_navigation %>
  ...
  <%= yield %>
</body>
</html>

这篇关于如何在 Rails 3 中创建动态生成的面包屑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆