Live data from Hacker News

Adding Breadcrumbs to a Rails Application

avohq.io

1–10 of 17 posts

Re: Adding Breadcrumbs to a Rails Application

#3
Here's my preferred approach, with breadcrumbs kept in erb views:

Make this view helper.

    def breadcrumb(&)
      render(layout: 'common/breadcrumb', &)
    end
Add this partial 'common/_breadcrumb.html.erb' (do whatever html you want):

    
      
    
Add this to your layout:

    
      
        
      
    
Then this is how you use it in your views:

    
      
      
      
    
For minitest tests I add this helper:

    module AssertBreadcrumbs
      Crumb = Struct.new(:text, :href)

      # Note: the block must have 1 argument per breadcrumb. It asserts their count.
      def assert_breadcrumbs(&blk)
        assert_select '.breadcrumb-item', blk.parameters.size do |items|
          structs = items.map { |item|
            if (link = item.css('a')[0])
              Crumb.new(link.text, link['href'])
            else
              Crumb.new(item.text.strip)
            end
          }

          yield(*structs)
        end
      end
    end
Which you can use in tests like this:

    assert_breadcrumbs do |item1, item2, item3|
      assert_equal 'Foo', item1.text
      assert_equal foo_url, item1.href

      assert_equal 'Bar', item2.text
      assert_equal bar_url, item2.href

      assert_equal 'you are here', item3.text
      assert_nil item3.href
    end

Re: Adding Breadcrumbs to a Rails Application

#5

Idk if there’s something wrong with me but I just can’t look at tailwind classes like that and think yep that looks good to me. Reminds me of the inline php days

I find the design aspect of stringing (primarily) defaults together very pleasing over the alternative of authoring ad hoc CSS/SASS/SCSS for every project.

Inlining it however, I'm with you.

Re: Adding Breadcrumbs to a Rails Application

#7

Idk if there’s something wrong with me but I just can’t look at tailwind classes like that and think yep that looks good to me. Reminds me of the inline php days

There’s nothing wrong with you, it’s obviously terrible.

Tailwind folks will tell you you’re holding it wrong, but every tailwind codebase I’ve seen winds up like this.

Re: Adding Breadcrumbs to a Rails Application

#8
post #7

Idk if there’s something wrong with me but I just can’t look at tailwind classes like that and think yep that looks good to me. Reminds me of the inline php days

There’s nothing wrong with you, it’s obviously terrible. Tailwind folks will tell you you’re holding it wrong, but every tailwind codebase I’ve seen winds up like this.

I mean the use of tailwind in the article is not good. Shows a lack of CSS understanding. Why are they applying `text-base` instead of just setting that on the root element? Why are they setting text color on the tag and then overriding it on the inside?

This person would write bad CSS, let's not put the blame on tailwind.

Also so much repetition instead of pulling each breadcrumb link out into a shared component. I understand it's just demo code for an article, but if all code bases end up like this that you've seen, the issue isn't tailwind.

Re: Adding Breadcrumbs to a Rails Application

#9
post #8
post #7

Earlier quoted context omitted.

There’s nothing wrong with you, it’s obviously terrible. Tailwind folks will tell you you’re holding it wrong, but every tailwind codebase I’ve seen winds up like this.

I mean the use of tailwind in the article is not good. Shows a lack of CSS understanding. Why are they applying `text-base` instead of just setting that on the root element? Why are they setting text color on the tag and then overriding it on the inside? This person would write bad CSS, let's not put the blame on tailwind. Also so much repetition instead of pulling each breadcrumb link out into a shared component. I…

My limited experience is that it's a fair bit harder to do a good job of reviewing PRs with tailwind versus CSS. So many classes tend to blur together in the markup.

Might just be me, but I'd rather just see clean(er) markup and styles in a css file.

Re: Adding Breadcrumbs to a Rails Application

#10
post #2

Somehow this code lacks the magic I‘m used from rails: class BooksController Only the title is specific to the show method. Home should be set by the application controller and Books by the books controller code.

I think it depends on how you look at things.

Here is what I like about this code:

1. It is explicit

2. Breadcrumbs are information that this action needs to set. You can set them in the views or in the controller via these helpers. But no matter where you put the data it is custom data that you as developer set and it is specific to this controller.

The information about how to navigate from homepage to this show method is something that either: you can use meta-programming to try to get it if you would for example scope controllers based on paths (not sure it is a good idea) or you have to provided as Rails cannot know if your controllers/views are in the top namespace.

Post reply on HN