Ruby on Rails中的面包屑 [英] Breadcrumbs in Ruby on Rails
本文介绍了Ruby on Rails中的面包屑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对我的面包屑解决方案有点不安全。在每个控制器操作中定义了名称和链接:
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 %>
这种方式我可以使用:
@l1_link = user_path()
问题:聪明 - 这种系统可以导致desaster在路下的某个地方吗?
Question: As I am not that smart - could this kind of system lead to desaster somewhere down the road? Is this (grossly) inefficient?
推荐答案
这主要是意见的问题,但无论如何:
This is mostly a matter of opinion, but anyway:
- 我不想在视图中看到那么多的逻辑。
- 代码对未来影响树深度的变化不安全。
- 而不是链接变量
* _ name
和* _ link
,我建议使用合适的对象, code> link_to 功能。
- I would not want that much logic in a view. We've probably all done it, but it gets messy quickly.
- The code is not safe against future changes that affect the depth of the tree.
- Instead of linked variables
*_name
and*_link
, I'd suggest using proper objects anyway, with somelink_to
functionality.
您可能会发现第61集Railscasts 感兴趣的一个很好的解决方案,通过
You might find Episode 162 of Railscasts of interest for a nice solution that gets by with
<% for page in @page.ancestors.reverse %>
<%= link_to h(page.name), page %> >
<% end %>
这篇关于Ruby on Rails中的面包屑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文