Breadcrumbs in Ruby on Rails

前端 未结 6 863
猫巷女王i
猫巷女王i 2021-02-01 07:02

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

Home         


        
6条回答
  •  暖寄归人
    2021-02-01 07:40

    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: " %>
    

提交回复
热议问题