问题
I am trying to embed additional HTML inside of a link_to call as found in this thread Embed additional HTML inside of link_to call
However, I would also like to use I18n. So instead of this:
<%= link_to '<i class="icon-search"></i> Show'.html_safe, exercise_path(exercise), :class => 'btn btn-small' %>
I would like to use t(:show)
or I18n.t(:show)
instead of a hardcoded Show
in the above example. I am having trouble figuring out the correct syntax, though. Any help would be greatly appreciated.
回答1:
There's an easier/cleaner way to embed additional items into a link_to
by using its block syntax. E.x.
<%= link_to exercise_path(exercise), :class => 'btn btn-small' do %>
<i class="icon-search"></i>
<%= t(:show).html_safe %>
<% end %>
回答2:
Use raw function just like below example
<%= link_to raw("<i class='icon-search'>some italic text </i> #{t(:show)}"), exercise_path(exercise), :class => 'btn btn-small' %>
回答3:
<%= link_to "<i class='icon-search'></i> #{t(:show)}".html_safe, exercise_path(exercise), :class => 'btn btn-small' %>
Use #{}
to embed ruby code in strings.
来源:https://stackoverflow.com/questions/9977715/ruby-on-rails-embed-additional-html-inside-of-link-to-call-that-includes-i18n