Breadcrumbs in Ruby on Rails

2019-03-09 15:24发布

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?

7条回答
欢心
2楼-- · 2019-03-09 15:58

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

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

<%= breadcrumbs pretext: "You are here: " %>
查看更多
登录 后发表回答