RESTful Rails应用程序的简单面包屑 [英] Easy breadcrumbs for RESTful rails application

查看:97
本文介绍了RESTful Rails应用程序的简单面包屑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何可以生成面包的辅助方法(默认轨道面包屑)动态浏览特定页面,而不必在RESTful应用程序中传递琐碎的参数?也就是说,是否可以根据用户访问的REST网址自动确定用户的位置?



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

REST

 <%add_crumb('个人资料,user_profile_path)%> 

当前页面

 <%add_crumb(我的传入邮件,request.path)%> 

必须有一种通用化代码的方式,这样就不需要参数传递,并且应该对所有人有效具有最少配置的RESTful应用程序。

解决方案

开发了一个简单的技巧。但是,该方法假定与RESTful网址中的每个资源相对应的每个模型对象都存在一个方法名称。方法名称返回的结果将显示为面包屑名称。如果未找到,则按原样显示它,而不使其链接到任何内容。分隔符为'->',您可以根据自己的需要进行更改。

  def get_bread_crumb(url)
开始
面包屑=''
沙发椅='/'
元素= url.split('/')
for i in 1 ... elements.size
沙发椅+ =元素[i] +'/'
,如果i%2 == 0
开始
面包屑+ =< a href ='#{sofar}'> + eval(#{elements [i-1] .singularize.camelize} .find(#{elements [i]})。name)。to_s +'< / a>'
抢救
面包屑+ =元素[i]
结束
其他
面包屑+ =< a href ='#{sofar}'>#{elements [i] .pluralize}< / a>
结束
面包屑+ =’-> '如果i!= elements.size-1
结束
面包屑
救援
'不可用'
结束
结束

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



<%= get_bread_crumb(request.url)%>



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



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



如果代码质量不是很好,请问好。我确定可以重构此代码,但是我也确定您可以在使用它之前做到这一点。



谢谢。


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.

解决方案

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.

这篇关于RESTful Rails应用程序的简单面包屑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆