Breadcrumbs in Ruby on Rails

前端 未结 6 906
猫巷女王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:46

    This is mostly a matter of opinion, but anyway:

    1. I would not want that much logic in a view. We've probably all done it, but it gets messy quickly.
    2. The code is not safe against future changes that affect the depth of the tree.
    3. Instead of linked variables *_name and *_link, I'd suggest using proper objects anyway, with some link_to functionality.

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

提交回复
热议问题