0

I can't manage to make it work... even using raw or html_safe

view

<%= button_to(glyphicon('heart', 'I love it !'), some_path, class: "btn btn-success")%>

helper

def glyphicon(glyph, text = nil)
    html = "<i class=\"glyphicon glyphicon-#{glyph}\"></i>"
    html += " #{text}" if text
    html.html_safe
end

result

A success btn with <i class= and " /> after

The following works (I've been doing it for ages), but the extra do syntax is annoying...

<%= button_to(some_path(@etude), class: "btn btn-success") do %>
  <i class="glyphicon glyphicon-heart"></i> I love it !
<% end %>

EDIT

Found a more compact syntax :

<%= button_to(some_path(@etude), class: "btn btn-success"){
      glyphicon('heart', 'I love it')} %>
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164

1 Answers1

0

I don't think its a problem with html safe, rather the name attribute for button_to will only accept plain text.

<%= button_to("<b>hello</b>".html_safe, 'http://www.google.com', :class => 'button', :method => 'get') %>

and

<%= button_to("<b>hello</b>", 'http://www.google.com', :class => 'button', :method => 'get') %>

both produce a button with <b>hello</b> written on it rather than just hello in bold.

The method using do is the only way to do it, it's the same thing you need to do when linking glyphicons via link_to.

But you could do this.

<%= button_to(some_path(@etude), class: "btn btn-success") do %>
  glyphicon('heart','I love it')
<% end %>
Dontreadonme
  • 334
  • 3
  • 13