用于 RESTful 轨道应用程序的简单面包屑

发布于 2024-07-13 22:27:20 字数 529 浏览 7 评论 0原文

是否有任何辅助方法(除了 默认 Rails 面包屑 之外)可以动态生成面包屑导航对于特定页面,无需在 RESTful 应用程序中传递简单参数? 也就是说,可以根据用户正在访问的 REST url 自动找出用户所在的位置?

对于上述实现,我们需要传递诸如

REST

<% add_crumb(‘Profile’, user_profile_path) %>

当前页面

<% add_crumb(“My Incoming Messages”, request.path) %>

类的参数,必须有一种方法来概括代码,以便不需要传递参数并且应该适用于所有 RESTful具有最少配置的应用程序。

Is there any helper method (Other than default rails breadcrumb) that generates bread crumb navigation dynamically for a particular page without having to pass trivial parameters in RESTful application? That is, something that figures out automatically where the user is based on the REST url she is visiting?

For above mentioned implementation, we need to pass parameters like

REST

<% add_crumb(‘Profile’, user_profile_path) %>

Current page

<% add_crumb(“My Incoming Messages”, request.path) %>

There must be a way to generalize the code so that no parameter passing is required and should work for all RESTful apps with minimal configuration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独行侠 2024-07-20 22:27:20

开发了一个简单的技巧。 然而,该方法假设与 RESTful url 中的每个资源相对应的每个模型对象都存在一个方法“名称”。 无论方法“名称”返回什么,都会显示为面包屑名称。 如果未找到,则会按原样显示,而不链接到任何内容。 使用的分隔符是“->” 您可以更改它以满足您的要求。

def get_bread_crumb(url)
  begin
    breadcrumb = ''
    sofar = '/'
    elements = url.split('/')
    for i in 1...elements.size
      sofar += elements[i] + '/'
      if i%2 == 0
        begin
          breadcrumb += "<a href='#{sofar}'>"  + eval("#{elements[i - 1].singularize.camelize}.find(#{elements[i]}).name").to_s + '</a>'
        rescue
          breadcrumb += elements[i]
        end
      else
        breadcrumb += "<a href='#{sofar}'>#{elements[i].pluralize}</a>"
      end
      breadcrumb += ' -> ' if i != elements.size - 1
    end
    breadcrumb
  rescue
    'Not available'
  end
end

该方法一般接受request.url(给定当前页面的url)作为参数。 该方法有目的地接受 url 以便进行自定义。 要生成面包屑,只需在视图中添加以下代码 -

<%= get_bread_crumb(request.url) %>

对于 URL /ideabox/2/idea/1,面包屑看起来像

替代文本 http://www.imagechicken.com/uploads/1234855404069992300 .png

如果代码质量不是很好,请原谅。 我确信这段代码可以被重构,但我也确信您能够在使用它之前做到这一点。

谢谢。

Developed a simple hack. The method however assumes that there exists a method 'name' for every model object corresponding to each resource in the RESTful url. Whatever that the method 'name' returns is shown as breadcrumb name. If it is not found, it is shown as it is without making it link to anything. Separator used is '->' You may change it to suit your requirement.

def get_bread_crumb(url)
  begin
    breadcrumb = ''
    sofar = '/'
    elements = url.split('/')
    for i in 1...elements.size
      sofar += elements[i] + '/'
      if i%2 == 0
        begin
          breadcrumb += "<a href='#{sofar}'>"  + eval("#{elements[i - 1].singularize.camelize}.find(#{elements[i]}).name").to_s + '</a>'
        rescue
          breadcrumb += elements[i]
        end
      else
        breadcrumb += "<a href='#{sofar}'>#{elements[i].pluralize}</a>"
      end
      breadcrumb += ' -> ' if i != elements.size - 1
    end
    breadcrumb
  rescue
    'Not available'
  end
end

The method generally accepts request.url (Which given url of the current page) as the parameter. The method purposefully accepts the url for customization purposes. To generate the breadcrumb, simply add following code in your view -

<%= get_bread_crumb(request.url) %>

For the url /ideabox/2/idea/1, the bread crumb looks like

alt text http://www.imagechicken.com/uploads/1234855404069992300.png

Excuse me if code quality is not that great. I'm sure this code can be re-factored but I'm also sure you would be able to do that before using it.

Thanks.

怎言笑 2024-07-20 22:27:20

chirantan 提供的解决方案很棒。 如果您需要命名空间控制器的面包屑,并且还需要根据命名空间更改面包屑,请尝试此操作。 这并不完美,但可以根据需要重构它。 它适用于我的项目。

定义一个新的助手: navigation_helper.rb

module NavigationHelper

  def navigation_add(title, url, namespace)

    if defined? @@namespace and !@@namespace.nil? and @@namespace == namespace
      @@navigation ||= []
    else
      @@navigation = []
    end

    @@navigation << {title: title, url: url} unless title == "Home"

    new_nav = []
    @@navigation.each do |hash|
      new_nav.push hash
      if hash[:title].to_s == title.to_s
        break
      end
    end

    @@navigation = new_nav
    @@navigation.uniq!
    @@namespace = namespace
  end

  def render_navigation
    if (request.path_parameters[:controller].sub('/', '::_').camelize + 'Controller').classify.constantize.action_methods.to_a.include? 'index'
      navigation_add controller_name.camelize.to_s, request.path_parameters.merge({action: 'index'}).except(:id), params[:controller].include?('/') ? params[:controller].split("/").first : nil
    end
    if defined? @@navigation
      render partial: 'navigation/navigation', locals: { navs: @@navigation, namespace: @@namespace }
    else
      render text: ''
    end
  end
end

然后为这个助手定义一个视图 _navigation.haml

- unless navs.blank?

  %ol.breadcrumb

    - navs.each_with_index do |nav, index|

      - if index == 0

        %li=link_to fa_icon('arrow-left', text: 'Go Back'), :back

        - unless namespace.nil?

          %li

            %h4.inline= request.fullpath.split('/')[1].gsub('-', '_').camelize

            = fa_icon('angle-double-right')

      %li= link_to_unless (nav[:title] == controller_name.camelize and action_name == 'index'),  fa_icon(nav[:title].downcase.singularize, text: nav[:title]), nav[:url]

The solution provided by chirantan is great. If you need breabcrumbs for namespaced controller and need to change the breadcrumbs depending on the namespace as well then try this. This is not perfect but refactor it as you need. It works for my project.

Define a new helper: navigation_helper.rb

module NavigationHelper

  def navigation_add(title, url, namespace)

    if defined? @@namespace and !@@namespace.nil? and @@namespace == namespace
      @@navigation ||= []
    else
      @@navigation = []
    end

    @@navigation << {title: title, url: url} unless title == "Home"

    new_nav = []
    @@navigation.each do |hash|
      new_nav.push hash
      if hash[:title].to_s == title.to_s
        break
      end
    end

    @@navigation = new_nav
    @@navigation.uniq!
    @@namespace = namespace
  end

  def render_navigation
    if (request.path_parameters[:controller].sub('/', '::_').camelize + 'Controller').classify.constantize.action_methods.to_a.include? 'index'
      navigation_add controller_name.camelize.to_s, request.path_parameters.merge({action: 'index'}).except(:id), params[:controller].include?('/') ? params[:controller].split("/").first : nil
    end
    if defined? @@navigation
      render partial: 'navigation/navigation', locals: { navs: @@navigation, namespace: @@namespace }
    else
      render text: ''
    end
  end
end

Then define a view for this helper _navigation.haml

- unless navs.blank?

  %ol.breadcrumb

    - navs.each_with_index do |nav, index|

      - if index == 0

        %li=link_to fa_icon('arrow-left', text: 'Go Back'), :back

        - unless namespace.nil?

          %li

            %h4.inline= request.fullpath.split('/')[1].gsub('-', '_').camelize

            = fa_icon('angle-double-right')

      %li= link_to_unless (nav[:title] == controller_name.camelize and action_name == 'index'),  fa_icon(nav[:title].downcase.singularize, text: nav[:title]), nav[:url]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文