Adding Breadcrumbs to a Rails Application
1–10 of 17 posts
Re: Adding Breadcrumbs to a Rails Application
#2 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.Re: Adding Breadcrumbs to a Rails Application
#3Make 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
endRe: Adding Breadcrumbs to a Rails Application
#4Re: Adding Breadcrumbs to a Rails Application
#5Idk 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
Inlining it however, I'm with you.
Re: Adding Breadcrumbs to a Rails Application
#6Re: Adding Breadcrumbs to a Rails Application
#7Idk 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
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
#8Idk 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.
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
#9Earlier 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…
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
#10Somehow 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.
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.