Skip to content

Instantly share code, notes, and snippets.

@seanharmer
Created December 10, 2020 08:36
Show Gist options
  • Save seanharmer/f0fb73170853c5628613ef90ed3b540c to your computer and use it in GitHub Desktop.
Save seanharmer/f0fb73170853c5628613ef90ed3b540c to your computer and use it in GitHub Desktop.
Rails font awesome xlink
# app/helpers/application_helper.rb
module ApplicationHelper
def declare_fa_icon(options, anchor_name)
fa_icon(options, data: { 'fa-symbol': anchor_name })
end
def use_fa_icon(anchor_name, text)
%Q(<svg class="icon-fa-xs"><use xlink:href="##{anchor_name}"></use></svg>#{text}).html_safe
end
end
# app/views/*/*.html.erb
# Declare commonly used icons
<%= declare_fa_icon('eye', 'fa-eye') %>
<%= declare_fa_icon('edit', 'fa-edit') %>
<%= declare_fa_icon('trash', 'fa-trash') %>
...
# Use icons declared above
<% @users.each do |user| %>
<%= link_to use_fa_icon('fa-eye', 'Show'), user_path(user), class: 'btn btn-sm btn-info' %>
<%= link_to use_fa_icon('fa-edit', 'Edit'), edit_user_path(user), class: 'btn btn-sm btn-warning' %>
<%= link_to use_fa_icon('fa-trash', "Delete"), user_path(user),
class: 'btn btn-sm btn-danger',
method: :delete, data: { confirm: 'Are you sure?' } %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment