Breadcrumbs in Ruby on Rails

Breadcrumbs in Ruby on Rails

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

How build a breadcrumbs in rails 4

My solution:

navigation_helper.rb

module NavigationHelper
def ensure_navigation
@navigation ||= [ { :title => 'Home', :url => '/' } ]
end

def navigation_add(title, url)
ensure_navigation << { :title => title, :url => url }
end

def render_navigation
render :partial => 'navigation', :locals => { :nav => ensure_navigation }
end
end

_navigation.html.erb

  <ol class="breadcrumb">
<% nav.each do |n| %>
<% unless n.equal? nav.last %>
<li><%= link_to n[:title], n[:url] %></li>
<% else %>
<li><%= n[:title] %></li>
<% end %>
<% end %>
</ol>

application.html.erb

<%= render_navigation %>

And any view:

<% content_for :title, 'My Page Title' %>
<% navigation_add @something.anything, '#' %>

how to add breadcrumb to devise ruby on rails

I'm using gem breadcrumbs on rails with devise in my project.

If you haven't made User model with devise make that first:

rails g devise User
rake db:migrate
rails generate devise:views users

My registration_controller.rb looks like this:

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
add_breadcrumb "home", :root_path
add_breadcrumb "contact", :contacts_path
end

I changed routes:

devise_for :users, :controllers => { registrations: 'registrations' }

In application.html.erb layout I added breadcrumbs (just above the <%= yield %> )

<%= render_breadcrumbs %>

I've just tested it, and it works as you can see from the screenshot.

Sample Image

EDITED:

In case that you want to add breadcrumbs to other pages of Devise gem, for example Forgot your password page, you can make new controller:

# app/controllers/passwords_controller.rb
class PasswordsController < Devise::PasswordsController
add_breadcrumb "home", :root_path
add_breadcrumb "contact", :contacts_path
end

and update your routes:

  devise_for :users, controllers: {
registrations: 'registrations',
passwords: 'passwords'
}

Please let me know if it works for you.

Breadcrumb in rails

Gretel is a very good gem for making breadcrumb in rails. It is very easy to use, you can find the tutorial on the same page.

Hope this helps.

Rails How can I build a breadcrumb with different urls for each element?

Firstly, let me note that you need to determine the logic of where you want to link to for each of the particular breadcrumb items.

As far as I can tell from your example, you are using the following convention to determine the link destination for a certain breadcrumb:

The link destination for the breadcrumb consists of:

  • a static path prefix (which in your case would be http://localhost:3000/int/en/admin/assets/),
  • the concatenation of all higher-or-same-level-breadcrumbs, joined by /. In case of item = 'company', that would be downloads/company,
  • and the static suffix /path.

In order to create these links you could use the following code:

<ol class="breadcrumb">
<li><%= link_to 'Start', admin_assets_path %></li>
<%
items = @prefix.split("/")
prefix = admin_assets_path
suffix = '/path'
%>
<% items.each.with_index do |item, i| %>
<%
middle = items[0..i].join('/')
path = prefix + middle + suffix
%>
<li> <%= link_to "/ #{item}", path %></li>
<% end %>
</ol>

However your actual question was:

How should be the logic to get every breadcrumb its own url?

Your current convention (breadcrumbs form links) has some limitations, for example internationalization is difficult and we cannot support cases where the logical breadcrumb structure does not mirror the url structure.

You might use something like a hash map or tree structure in order to model the actual site structure or have a look at gems made for this purpose, for example breadcrumbs_on_rails.

Breadcrumbs on Rails with Tailwind

If you are using Tailwind v3, the classes are "purged" by default.

Since this is a ruby helper, I'd assume that this particular file was not added to the content list in tailwind.config.js.

Perhaps try adding something like this your config file:

module.exports = {
content: [
"./app/views/**/*.html.erb",
"./app/helpers/**/*.rb",
"./app/javascript/**/*.js",
"path/to/your/file.rb"
],
// ... your other configs
}

Hope that helps!

Ruby on Rails Link to Previous Page on Form Failing After Invalid Input

you could set a hidden_field on your form to cache url_for(:back) on the first time (that is the previous link before the form page), as below:

<%= breadcrumb t(".back"), params.dig(:bread,:back_url) || url_for(:back) %>

<%= form_with(model: bread, local: true) do |form| %>
<%= form.hidden_field :back_url, value: params.dig(:bread,:back_url) || url_for(:back) %>
# the rest of form ...
<% end %>


Related Topics



Leave a reply



Submit