2

I am currently using link_to:

<%= link_to edit_webcast_path(@webcast), :class => 'btn'  do %>
    <i class="icon-pencil"></i> Edit Webcast
<% end %>

But I want to do the same with button_to, however button_to must have a first label argument passed to it or it will use the path as its label.

<%= button_to edit_webcast_path(@webcast), :class => 'btn'  do %>
    <i class="icon-pencil"></i> Edit Webcast
<% end %>

... results in a button with a label that is the resolved edit_webcast_path(@webcast) path. In effect do seems to have no effect.

I have tried passing html directly as the first argument:

<%= button_to "<i class=\"icon-white icon-minus-sign \"></i> Edit Webcast".html_safe, webcast_path(@webcast), :class => 'btn'%>

But this results in a button with the label displaying part of the html string I pass in. Looking at the generated HTMl I can see that the contents of this first argument is placed in the input's value attribute and not inside the input itself.

How can I get this to work?

James Allardice
  • 164,175
  • 21
  • 332
  • 312
Undistraction
  • 42,754
  • 56
  • 195
  • 331

4 Answers4

2

Rails button_to generates a form with html <input type="submit"> element , not <button> one. You can try to override it or write your own helper to generate <button> , take a look at this example

Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206
dimuch
  • 12,728
  • 2
  • 39
  • 23
0

In Rails, button_to will try to do a post or put, so it is not actually a "regular" button, you can style your link to look and act like a button

YaBoyQuy
  • 783
  • 5
  • 8
0

You can add HTML inside a button_to element like this:

<%= button_to "New", new_articles_path do %>
  Create a <strong>new article</strong>
<% end %>

More info: https://apidock.com/rails/ActionView/Helpers/UrlHelper/button_to

penguoir
  • 155
  • 1
  • 11
-5

You are doing it wrong. You are trying to add a unnecessary html element to control something that is the CSS job.

Instead just create a class or Id for the button and add this to the css:

 .btn { padding-left:30px; background: url(pencil.png) no-repeat; }