How can I use erb in my ruby on rails view to make a bootstrap glyphicon a button?

512 Views Asked by At

I want to use a bootstrap glyphicon as a link in my rails app. To do this without a glyphicon I would use the link_to method: <%= link_to "up", like_post_path(post), method: :put %>

I basically want the "up" to be a glyphicon instead. But I can not figure out how to add a link and still preserve the glyphicon as is. Here is all the code in my erb view relevant to my desired button:

<a href="" class="btn btn-success" aria-label="vote up" type="button">
       <span class="glyphicon glyphicon-plus"></span>
</a>

I want to make this button link to my like_post_path(post). I've tried making the span into a button with the glyphicon classes, and a link_to method in the middle..didn't work. I've tried making the a href="<% many different erb attempts %> and no luck with that either. Can anyone tell me the proper way to do this?

2

There are 2 best solutions below

1
On BEST ANSWER

Simply use content_tag:

<%= button_to content_tag(:span, "", class: "glyphicon glyphicon-plus"), like_post_path(post), class: "btn btn-success" %>

You can also use it with link_to:

<%= link_to content_tag(:span, "", class: "glyphicon glyphicon-plus"), like_post_path(post), class: "btn btn-success", type: "button" %>

Or you can write the HTML code directly, and use the raw helper to avoid the ERB escape.

2
On

This will do

<%= link_to like_post_path(post), method: :put do %>
<button class="btn btn-success" aria-label="vote up"><span class="glyphicon glyphicon-plus"></span></button>
<% end %>