piotrmurach / loaf

Manages and displays breadcrumb trails in Rails app - lean & mean.
MIT License
407 stars 24 forks source link

Cannot have html tags as name #27

Closed alphamarket closed 6 years ago

alphamarket commented 6 years ago

let's say I have the following:

breadcrumb '<span class="fa fa-home"></span>', :root_path

The output is: image

I have tried raw|html_safe on the name, but result is the same!

piotrmurach commented 6 years ago

The breadcrumb name isn't changed by anything in the codebase, it is a simple value. Rails marks all strings as unsafe. I would apply raw, not when you specify breadcrumb but when you render it in a view.

<%= link_to raw(crumb.name), crumb.url, .... %>

However, I would really question using html in Rails controller, it feels wrong, it would be better to use a placeholder value, e.i. breadcrumb :home_icon, :root_path and then in your view:

<% if crumb.name == :home_icon %>
  <span class="fa fa-home"></span>
<% end %>

This way you will avoid HTML in your controller and having unsafe strings, there is a reason why it is not working out of the box in Rails.

alphamarket commented 6 years ago

for some reasons I need to have only this, I have alread put the raw when I render the item, the result is still the same!

<%= link_to crumb.url, (crumb.current? ? {'aria-current' => 'page'} : {}) do %>
  <%= raw crumb.name %>
<% end %>

This way you will avoid HTML in your controller and having unsafe strings, there is a reason why it is not working out of the box in Rails.

I am no so stricked guy; I allow myself to have some liberty in using html in my controller (life is easy this way) but I've never had this issue before.

piotrmurach commented 6 years ago

Having looked at your example again I have realised that by default each breadcrumb is truncated to 30 chars, this was based on the assumption that there will only be ever a name without HTML markup etc..

However, I think this arbitrary limit does more harm than good and this gem shouldn't need to worry about such things since my preference would be for people to control how they display things in their views and keep names as simple data.

Therefore, I went ahead and removed the truncation and capitalization of titles from the library, which as a side effect will allow you to put virtually anything as a title! - for the no so strict guy 😜

Please install v0.8.0 and let me know if you problem gets resolved.

alphamarket commented 6 years ago

Yes, the problem got fixed, Thanks!