42

How can I add an icon to my Rails application (to show in tabs when opened in a browser, etc.)?

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Mo.
  • 40,243
  • 37
  • 86
  • 131

5 Answers5

117

You can use Favicon Rails helper:

<%= favicon_link_tag %>

Or if you want another image than favicon.ico

<%= favicon_link_tag 'another_image.ico' %>
JCorcuera
  • 6,794
  • 2
  • 35
  • 29
  • 1
    Simple, but great 'as the rails way' answer. – Ricardo Castañeda Mar 10 '12 at 08:49
  • 9
    I'd just like to add that the favicon.ico file should go in the `public` directory and documentation can be found here: http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/favicon_link_tag – David Grayson Sep 19 '12 at 22:20
  • 1
    You have to reload the page skipping the cache. `Cmd+Shift+R` or `Ctrl+Shift+R` to view the effect. – Chris.Zou Aug 19 '15 at 08:09
  • 2
    **NOTE** Place the favicon.ico image in `/app/assets/images/` if you are using the asset pipeline, and in `/public/images/` if you are not. – Phil Feb 15 '16 at 19:36
  • @Chris.Zou, thanks for the tip! I have been struggling to find out why my icon didn't show up until I saw your comment – Sam Kah Chiin May 23 '20 at 06:47
35

You're talking about a favicon.

Add this in the head part of your layout:

<link rel="shortcut icon" href="/path_to_your_pic"/>
apneadiving
  • 114,565
  • 26
  • 219
  • 213
11

Just so people know, the "document root" where you place your favicon.ico is the "public" folder. As a Rails newb I thought it would be in the root of the entire application.

Also, you may have to clear your cache for it show up.

LennonR
  • 1,433
  • 18
  • 35
9

After reading and trying all this answers without success I ended up doing this

  1. Add a file favicon.png to your public/assets folder
  2. In your $/app/views/layouts/application.html.erb modify the content of the <head> tag adding this <link rel="icon" type="image/png" href="/assets/favicon.png">

No need to change the web_server configuration file (nginx, apache, etc) no need to precompile the assets.

Just stop and run/debug your web site, clear the cache from your browser and reopen the page. It should work

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
4

Put the favicon.ico under your public/ folder and then add <%= favicon_link_tag '/favicon.ico' %> to your <head></head>

Then if you try it and doesn't work out, even after you cleaning browser's cache, you should try run the server at a different port. By default, rails runs the server at port 3000.

Try changing the port to something you haven’t used before – run the app as:

RAILS 3: rails server -p 12345

RAILS 1/2: ruby script/server -p 12345

jbatista
  • 964
  • 2
  • 11
  • 26