2

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.

Community
  • 1
  • 1
diasks2
  • 2,033
  • 2
  • 36
  • 61

3 Answers3

9

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 %>
John
  • 3,296
  • 2
  • 24
  • 36
0

<%= 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.

Robin
  • 21,667
  • 10
  • 62
  • 85
  • Thank you, but when I use double quotes as in your answer I get a syntax error, and if I use single quotes with `<%= link_to ' #{t(:show)}'.html_safe, exercise_path(exercise), :class => 'btn btn-small' %>`, the embedded ruby doesn't work. Any ideas what I am doing wrong? – diasks2 Apr 02 '12 at 14:05
  • Sorry, for my solution to work, you would have needed to do `class='icon-search'` with single quotes. But johnernaut's answer is better anyway ;) I don't think you need the html_safe in his answer though. – Robin Apr 02 '12 at 14:56
0

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' %>
Muhammad Sannan Khalid
  • 3,127
  • 1
  • 22
  • 36