简体   繁体   中英

How to integrate Emoji into link_to

I want to use an Emoji, eg Trash-Emoji (HTML Entity: & #x1f5d1;) inside link_to, like:

<%= link_to "&#x1f5d1;", tape_path(tape.id), method: :delete, data: { confirm: 'Are you sure to delete?' }, class: 'btn btn-default btn-sm btn-warning' %>

But no Trash-Emoji itself is showing. I also tried "& #x1f5d1;", #{& #x1f5d1;}, & #x1f5d1; but syntac errors were shown.

I want to try to write without using any additional GEM at first. Does someone know how should be written?

html_safe and raw are not very flexible approaches when you want to make a really customized link

It is more elegant to use block where you can do A LOT:

<%= link_to root_path do %>
  Homepage
  <div class="fa fa-flag"></div>
  &#x1f5d1;
  <%= User.count %>
<% end %>

https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

Your case:

<%= link_to root_path, method: :delete, data: { confirm: 'Are you sure to delete?' }, class: 'btn btn-default btn-sm btn-warning' do %>
  &#x1f5d1;
<% end %>

我解决了这个问题,现在使用“link_to raw”!

<%= link_to raw("&#x1f5d1;").html_safe, tape_path(tape.id), method: :delete, data: { confirm: 'Are you sure to delete?' }, class: 'btn btn-default btn-sm btn-warning' %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM