Closed ScottKolo closed 8 years ago
To elaborate, this would allow a user to include HTML in a column label. My specific issue: I'd like to specify a different sort ascending/descending icon using Font Awesome. For example:
@filter_opts = {
:ascending_indicator => '<i class="fa fa-sort-asc" aria-hidden="true"></i>'.html_safe,
:descending_indicator => '<i class="fa fa-sort-desc" aria-hidden="true"></i>'.html_safe,
}
Seems reasonable, but here's the problem (from action_view_extension.rb):
link_to(
[opts[:label], opts[:current_sort_direction_indicator]].join(' '),
url_for(url_for_attrs),
opts[:html_attrs].reverse_merge(:class => css_classes, :method => :get, :remote => true)
)
.join(' ')
ends up removing any trace of HTML safety, so when the string is finally rendered by the link_to
, Rails assumes it needs to be sanitized and does so. Instead of inserting the HTML into the view and rendering the icon, it prints the escaped version in all its glory. safe_join
fixes this - if you send it a string marked with .html_safe
, it will not escape that string - but it will still escape all other strings being concatenated.
For more information on safe_join, feel free to check out the Rails documentation on it.
Thank you!
@ScottKolo can you elaborate on how you implemented that? Specifically where you added the @filter_opts setting. I was looking to do the exact same thing. Actually I was looking to see how to set these as a default for my whole app when I cam across this issue. The docs don't seem to document any of these options at all.
@aximuseng In your view, whenever you use filterrific_sorting_link
, you can pass in an optional opts
hash. You can find the code for filterrific_sorting_link
here:
https://github.com/jhund/filterrific/blob/master/lib/filterrific/action_view_extension.rb#L66
Just specify an opts
hash with anything you'd like to customize, and defaults are used for anything you haven't specified.
@ScottKolo I don't think I explained my question clearly - I understand the opts hash. That's easy - on your post your you set that @filter_opts variable. After re-reading it I am guessing you are doing this:
filterrific_sorting_link(filterrific, sort_key, opts = { @filter_opts })
I cam across your post looking to set this globally for the entire app. I am surprised that there is not an initializer file for this gem to set these default etc. I think I will just set @filter_opts as application helper method.
join concatenates array elements to create a string with the optional specified separator. Unfortunately, the resulting string is assumed not HTML-safe, and so Rails escapes it to be safe. Rails offers a better alternative, safe_join, that will concatenate elements while retaining each element’s HTML-safety and returning an HTML-safe string.