105

I'm using Twitter's Bootstrap stuff and I have the following HTML:

<a class="btn" href="<%= user_path(@user) %>"><i class="icon-ok icon-white"></i> Do it@</a>

What's the best way to do this in Rails? I'd like to use <%= link_to 'Do it', user_path(@user) %> but the <i class="icon-ok icon-white"></i> is throwing me off?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Vanessa L'olzorz
  • 1,325
  • 3
  • 10
  • 10

12 Answers12

274

Two ways. Either:

<%= link_to user_path(@user) do %>
  <i class="icon-ok icon-white"></i> Do it@
<% end %>

Or:

<%= link_to '<i class="icon-ok icon-white"></i> Do it@'.html_safe, user_path(@user) %>
Veraticus
  • 15,944
  • 3
  • 41
  • 45
16

I had the same need recently. Try this:

<%= link_to '<i class="icon-ok icon-white"></i> Do it'.html_safe, user_path(@user) %>

Eric Farkas
  • 531
  • 5
  • 9
11

You have also the possibility to create an helper method like below:

def link_fa_to(icon_name, text, link)
  link_to content_tag(:i, text, :class => "fa fa-#{icon_name}"), link
end

Adapt the classes to your needs.

Renshuki
  • 191
  • 2
  • 7
9

In normal HTML we do,

<a href="register.html"><i class="fa fa-user-plus"></i> Register</a>

In Ruby On Rails:

<%= link_to routeName_path do %>
  <i class="fa fa-user-plus"></i> Link Name
<% end %>

<%= link_to register_path do %>
   <i class="fa fa-user-plus"></i> Register
<% end %>

This is My Output

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ovi
  • 179
  • 1
  • 4
8

If you want a link in rails that uses that same icon class from twitter bootstrap all you need to do is something like this.

<%= link_to "Do it@", user_path(@user), :class => "btn icon-ok icon-white" %>
Justin Herrick
  • 2,981
  • 17
  • 18
  • 2
    @PeterNixey no it does not, it makes it look like a button. If you leave of the `btn` class, all you see is the icon. The button look doesn't mean it's a button. – Webdevotion Oct 09 '12 at 17:46
7

Using HAML:

= link_to model_path do
  %img{src: '/assets/someimg.png'}
ddavison
  • 28,221
  • 15
  • 85
  • 110
6

In the gem twitter-bootstrap-rail : they create a helper glyph

  def glyph(*names)
    content_tag :i, nil, :class => names.map{|name| "icon-#{name.to_s.gsub('_','-')}" }
  end

So you can use it like: glyph(:twitter) and you link helper could look like: link_to glyph(:twitter), user_path(@user)

eveevans
  • 4,392
  • 2
  • 31
  • 38
  • you could allow multiple clases for the a tag... In all cases, wich will be the use cases? – eveevans Feb 11 '13 at 20:30
  • 1
    This is a great way to create a link with a glyph (Font Awesome)! To add more classes use something like `<%= link_to glyph(:comments), post_path(post), :class => "btn-small btn-warning" %>`. Here `comments` is the name of the Font Awesome character, `post_path(post)` is the destination url and `class =>` shows which classes the glyph will use. – Weston Oct 14 '13 at 00:33
3

I will give this a shot since you haven't accepted an answer yet
and the other answers are not 100% what you were looking for.
This is the way to do it the Rails way.

<%= link_to(user_path(@user), :class => 'btn') do %>
  <i class="icon-ok icon-white"> </i> Do it!
<% end %>

Edit: leaving my answer for future reference,
but @justin-herrick has the correct answer when
working with Twitter Bootstrap.

Webdevotion
  • 1,223
  • 13
  • 24
2

I think you can simplified it through a helper method if you use it frequently in your application.

put it in helper/application_helper.rb

def show_link(link_text, link_source)
  link_to("#{content_tag :i, nil, class: 'icon-ok icon-white'} #{link_text}".html_safe,
    link_source, class: "btn")
end

Then call it from your view file just like link_to

<%= show_link "Do it", user_path(@user) %>
2

If you are using the bootstrap 3.2.0, you can use this helper in your app/helpers/application_helper.rb

module ApplicationHelper
  def glyph(*names)
    content_tag :i, nil, :class => names.map{|name| "glyphicon glyphicon-#{name.to_s.gsub('_','-')}" }
  end
end

and then, in your views:

link_to glyph(:pencil) + ' Edit', edit_post_path(@post), class: 'btn btn-warning'
Kleber S.
  • 8,110
  • 6
  • 43
  • 69
1
def show_link (source, text)
  link_to source, {'data-original-title' => 'Show', 'data-toggle' => 'tooltip', :class => 'btn btn-xs btn-success'} do
    "#{text} #{content_tag :i, nil, class:' glyphicon glyphicon-eye-open' }".html_safe
    end
end
mr i.o
  • 952
  • 2
  • 10
  • 20
0

Helper based on Titas Milan's suggestion, but using a block:

def show_link(link_text, link_source)
  link_to link_source, { class: 'btn' } do
    "#{content_tag :i, nil, class: 'icon-ok icon-white'} #{link_text}".html_safe
  end
end
meleyal
  • 32,252
  • 24
  • 73
  • 79