Ruby on Rails 中的面包屑导航

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2280648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:18:28  来源:igfitidea点击:

Breadcrumbs in Ruby on Rails

ruby-on-railsrubycontrollerbreadcrumbs

提问by Michael Schmitz

I'm slightly insecure about my breadcrumb solution. Names and links are defined in each controller action:

我对我的面包屑解决方案有点不安全。名称和链接在每个控制器操作中定义:

<a href="http://localhost:3000/">Home</a>
<% if defined? @l1_link %>
  > <a href="<%= @l1_link%>"><%= @l1_name %></a>
  <% if defined? @l2_link %>
    > <a href="<%= @l2_link%>"><%= @l2_name %></a>
  <% end %>
<% end %>

This way I can use:

这样我可以使用:

@l1_link = user_path()

Question: As I am not that smart - could this kind of system lead to desaster somewhere down the road? Is this (grossly) inefficient?

问题:由于我不是那么聪明——这种系统会导致未来某个地方的灾难吗?这(严重)效率低下吗?

采纳答案by Christopher Creutzig

This is mostly a matter of opinion, but anyway:

这主要是一个意见问题,但无论如何:

  1. I would not want that much logic in a view. We've probably all done it, but it gets messy quickly.
  2. The code is not safe against future changes that affect the depth of the tree.
  3. Instead of linked variables *_nameand *_link, I'd suggest using proper objects anyway, with some link_tofunctionality.
  1. 我不希望在视图中有那么多逻辑。我们可能都做过,但很快就会变得一团糟。
  2. 该代码对于影响树深度的未来更改是不安全的。
  3. 无论如何,我建议使用具有某些功能的适当对象,而不是链接变量*_nameand 。*_linklink_to

You might find Episode 162 of Railscastsof interest for a nice solution that gets by with

您可能会发现感兴趣的 Railscasts 第 162 集,这是一个很好的解决方案

<% for page in @page.ancestors.reverse %>
  <%= link_to h(page.name), page %> &gt;
<% end %>

回答by Simone Carletti

Breadcrumbs menu are a recurrent pattern in most Rails applications. To solve this issue, I created and released a plugin called breadcrumbs_on_rails.

面包屑菜单是大多数 Rails 应用程序中的循环模式。为了解决这个问题,我创建并发布了一个名为breadcrumbs_on_rails的插件。

You define your breadcrumbs in the controller

您在控制器中定义面包屑

class MyController

  add_breadcrumb "home", root_path
  add_breadcrumb "my", my_path

  def index
    # ...

    add_breadcrumb "index", index_path
  end

end

and you render them in your view.

然后在您的视图中呈现它们。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>untitled</title>
</head>

<body>
  <%= render_breadcrumbs %>
</body>
</html>

Even if you don't want to use a plugin, I encourage you to give it a look. It's open source and you can grab some idea for your app.

即使您不想使用插件,我也鼓励您看一看。它是开源的,您可以为您的应用程序获取一些想法。

回答by Lasse Bunk

I made a gem named Gretelthat is a Ruby on Rails plugin for creating breadcrumbs. The breadcrumbs are configured in a separate configuration file and selected in the view.

我制作了一个名为Gretel的 gem ,它是一个用于创建面包屑的 Ruby on Rails 插件。面包屑在单独的配置文件中配置并在视图中选择。

Example config/breadcrumbs.rb:

示例config/breadcrumbs.rb

crumb :root do
  link "Home", root_path
end

crumb :projects do
  link "Projects", projects_path
end

crumb :project do |project|
  link project.name, project_path(project)
  parent :projects
end

crumb :project_issues do |project|
  link "Issues", project_issues_path(project)
  parent :project, project
end

crumb :issue do |issue|
  link issue.name, issue_path(issue)
  parent :project_issues, issue.project
end

In your view:

在您看来:

<% breadcrumb :issue, @issue %>

In your app/views/layouts/application.html.erb:

在您的 app/views/layouts/application.html.erb 中:

<%= breadcrumbs pretext: "You are here: " %>

回答by user1449337

Do not use any plugins just for breadcrumbs. This link provides an efficient method to generate breadcrumbs.

不要只为面包屑使用任何插件。此链接提供了一种生成面包屑的有效方法。

http://szeryf.wordpress.com/2008/06/13/easy-and-flexible-breadcrumbs-for-rails/

http://szeryf.wordpress.com/2008/06/13/easy-and-flexible-breadcrumbs-for-rails/

Although, it is a very old post, it still works.

虽然这是一个很老的帖子,但它仍然有效。

回答by sekrett

I did a fork of crumblegem. It has very few configuration options and it appears abandoned, but when I tried to switch to breadcrumbs_on_railsor gretel, I realized that I have to add many lines to my views or controllers (and I have a lot of them), but with crumbleit is all stored in one configuration file. If you like to do some patches and prefer configuration in one place, I think it is the best solution ever.

我做了一个crumble宝石叉。它的配置选项很少,似乎被放弃了,但是当我尝试切换到breadcrumbs_on_railsor 时gretel,我意识到我必须向我的视图或控制器添加许多行(而且我有很多),但是crumble所有这些都被存储了在一个配置文件中。如果你喜欢做一些补丁并且喜欢在一个地方配置,我认为这是有史以来最好的解决方案。

回答by Happynoff

You could also use Ariane http://github.com/simonc/ariane

你也可以使用 Ariane http://github.com/simonc/ariane

With it you can generate any kind of breadcrumb, as links in a paragraph or as a ul/li :)

有了它,您可以生成任何类型的面包屑,作为段落中的链接或作为 ul/li :)

If you want something specific, you can create your own renderer.

如果你想要一些特定的东西,你可以创建自己的渲染器。

It's pretty simple to use, just add this in a before_filter:

使用起来非常简单,只需将其添加到before_filter

ariane.add 'Home', root_path # in the app controller to have it everywhere
ariane.add 'Some Page', some_path