首页
/ Crummy 面包屑导航技术文档

Crummy 面包屑导航技术文档

2024-12-23 02:20:15作者:瞿蔚英Wynne

本文档将详细介绍如何在 Rails 应用中安装、使用和配置 Crummy 插件,以添加面包屑导航功能。

1. 安装指南

在 Rails 应用的 Gemfile 文件中添加以下依赖项:

gem "crummy", "~> 1.8.0"

然后执行以下命令安装该 gem:

bundle install

2. 项目的使用说明

Crummy 插件允许您在 Rails 控制器和视图中添加和管理面包屑导航。

控制器中使用

在控制器中,您可以使用 add_crumb 方法添加面包屑:

class ApplicationController
  add_crumb "首页", '/'
end

class BusinessController < ApplicationController
  add_crumb("业务", lambda { |instance| instance.businesses_path })
  add_crumb("评论", only: "comments", lambda { |instance| instance.businesses_comments_path })
  before_action :load_comment, only: "show"
  add_crumb :评论, only: "show"

  # 为嵌套路由添加面包屑
  add_crumb(:文档) { [:account, :document] }

  def show
    add_crumb @business.display_name, @business
  end

  def load_comment
    @comment = Comment.find(params[:id])
  end
end

视图中渲染

在视图中,使用 render_crumbs 方法渲染面包屑:

<%= render_crumbs %>

面包屑链接 HTML 选项

您可以通过 link_html_options 方法设置面包屑链接的 HTML 选项:

add_crumb "首页", '/', link_html_options: {title: "我的链接标题"}

使用 HTML 代替文本

您可以在第一个参数中使用 HTML 代替文本。如果 HTML 中包含 <a> 标签,该标签将不会被用作包装器:

add_crumb "<a class='glyphicons shield' href='/support'><i></i>支持</a>".html_safe, "", {}

3. 项目 API 使用文档

以下是 render_crumbs 方法的可用选项:

  • format: 输出格式,可以是 :html:html_list:xml。默认为 :html
  • separator: 分隔符文本。默认为 &raquo;(对于 :html)和 <crumb>(对于 :xml)。
  • links: 是否渲染链接。默认为 true
  • microdata: 是否渲染 Richsnipet。默认为 false
  • last_crumb_linked: 是否链接最后一个面包屑。默认为 true
  • skip_if_blank: 如果没有面包屑,输出为空。默认为 false

以下是一些示例:

render_crumbs                     # => <a href="/">首页</a> &raquo; <a href="/businesses">业务</a>
render_crumbs separator: ' | '    # => <a href="/">首页</a> | <a href="/businesses">业务</a>
render_crumbs format: :xml        # => <crumb href="/">首页</crumb><crumb href="/businesses">业务</crumb>
render_crumbs format: :html_list  # => <ol class="" id=""><li class=""><a href="/">首页</a></li><li class=""><a href="/businesses">业务</a></li></ol>

4. 项目安装方式

请参考上述“安装指南”部分。

以上是 Crummy 面包屑导航插件的技术文档,希望对您在使用该插件时有所帮助。

登录后查看全文
热门项目推荐

项目优选

收起
openGauss-serveropenGauss-server
openGauss kernel ~ openGauss is an open source relational database management system
C++
138
189
RuoYi-Vue3RuoYi-Vue3
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
887
528
openHiTLSopenHiTLS
旨在打造算法先进、性能卓越、高效敏捷、安全可靠的密码套件,通过轻量级、可剪裁的软件技术架构满足各行业不同场景的多样化要求,让密码技术应用更简单,同时探索后量子等先进算法创新实践,构建密码前沿技术底座!
C
370
383
ohos_react_nativeohos_react_native
React Native鸿蒙化仓库
C++
184
265
kernelkernel
deepin linux kernel
C
22
5
MateChatMateChat
前端智能化场景解决方案UI库,轻松构建你的AI应用,我们将持续完善更新,欢迎你的使用与建议。 官网地址:https://matechat.gitcode.com
735
105
note-gennote-gen
一款跨平台的 Markdown AI 笔记软件,致力于使用 AI 建立记录和写作的桥梁。
TSX
84
4
CangjieCommunityCangjieCommunity
为仓颉编程语言开发者打造活跃、开放、高质量的社区环境
Markdown
1.08 K
0
harmony-utilsharmony-utils
harmony-utils 一款功能丰富且极易上手的HarmonyOS工具库,借助众多实用工具类,致力于助力开发者迅速构建鸿蒙应用。其封装的工具涵盖了APP、设备、屏幕、授权、通知、线程间通信、弹框、吐司、生物认证、用户首选项、拍照、相册、扫码、文件、日志,异常捕获、字符、字符串、数字、集合、日期、随机、base64、加密、解密、JSON等一系列的功能和操作,能够满足各种不同的开发需求。
ArkTS
61
2
HarmonyOS-ExamplesHarmonyOS-Examples
本仓将收集和展示仓颉鸿蒙应用示例代码,欢迎大家投稿,在仓颉鸿蒙社区展现你的妙趣设计!
Cangjie
400
377