Ruby on Rails - embed additional HTML inside of link_to call that includes I18n

拜拜、爱过 提交于 2019-12-12 18:23:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!