How to add favicon in rails 3.2
Ruby on-RailsFaviconRuby on-Rails Problem Overview
I know new rails apps come with an empty favicon.ico file. I want to know how I go about adding a favicon. I know you can use the favicon_link_tag
helper, but I am not sure how to populate the favicon.ico file. Do you use favicon generators? If so, which one is best?
I also want to be able to cache it, does rails do that automatically as well?
Thanks
Ruby on-Rails Solutions
Solution 1 - Ruby on-Rails
Simply add this to the <head></head>
section of your layouts:
<%= favicon_link_tag 'favicon.ico' %>
Place the favicon.ico image in /app/assets/images/
if you are using the asset pipeline, and in /public/images/
if you are not.
Also, there is a bug if using Ruby 2.0 with Rails 3.0.20 (and maybe also 3.0.x), that will throws an exception when trying to render favicon.ico.
The fix is to place the following code into application_controller.rb:
config.relative_url_root = ""
Solution 2 - Ruby on-Rails
generate your favicon for example here: http://www.favicon.cc/ and put in to public/ directory
UPDATE Favicon in public folder is not precompiled and it may be cached for a long time. It looks like it is better to use favicon_link_tag to avoid favicon updating problems. I do not know browsers needed favicon in root. According to favicon wiki all modern browsers maintains
<link rel="shortcut icon" href="favicon path" /> (favicon_link_tag)
Solution 3 - Ruby on-Rails
While all these answers are saying to create a 16x16 icon, the reality is you should be creating both a 16x16 and 32x32, in order to support retina displays. None of the online generators did a very good job with this.
On Mac, there is a great $5 app called Icon Slate, which allows you to easily create both formats in a single ICO file.
On Windows, I've used Axialis IconWorkshop with great success, but it's a much heavier-duty tool, and is significantly more expensive at about €50.
Both will create an ico file with both 16x16 and 32x32 images within it.
If you're using the asset pipeline, use the app/assets/images folder rather than /public. The number of fringe browsers that ignore the link
tag is rapidly approaching zero, so jumping through hoops to accommodate them isn't worth it.
As mentioned in other answers, use this in the head
to display it:
<%= favicon_link_tag 'favicon.ico' %>
Solution 4 - Ruby on-Rails
I highly recommend this option. It was easy to use and free http://converticon.com
Solution 5 - Ruby on-Rails
write in application.html.haml:
= favicon_link_tag '/images/favicon.ico'
place file favicon.ico in dir:
project/public/images
Solution 6 - Ruby on-Rails
You pretty much need a 16x16 pixel image file called favicon.ico and it must be available publically in the root of your site.
You can always use a major image editor to convert your logo or other image to the .ico format. There are free options like Gimp that can make such great icons based on existing image better than online icon generators.
Solution 7 - Ruby on-Rails
I tried the links above and the services were not very simple to use. I find this link on another site and it copied over my .png file flawlessly and was very simple to use. I thought I would share this link too, as I think it is a better service.
Solution 8 - Ruby on-Rails
Haven't done it for years, but Gimp is capable of saving .ico files with multiple images having different sizes. You just need to export to .ico format with some visible layers.
Solution 9 - Ruby on-Rails
To generate a favicon for all platforms (not only for desktop browsers), you can use RealFaviconGenerator and the rails_real_favicon gem:
- Go to RealFaviconGenerator and submit your picture. You can craft your icon, platform per platform: iOS, Android, etc.
- Once your icon is ready, click the "Rails" tab to get the steps to install your favicon in your Rails project. Basically, you will be asked to:
- Add the
rails_real_favicon
gem to youGemfile
- Create a new file named
favicon.json
. This file describes the icons you've just designed. - Run
rails generate favicon
to actually create the icons and HTML code. - Add
render 'favicon'
in your layouts to insert the HTML code in your pages.
- Add the
The advantage of this solution is that it injects the favicon files (favicon.ico
, apple-touch-icon.png
, but also browserconfig.xml
and manifest.json
) in the asset pipeline.
Full disclosure: I'm the author of RealFaviconGenerator.
Solution 10 - Ruby on-Rails
The solution I found that worked for me was to do the following:
- Go to http://realfavicongenerator.net/favicon_checker and confirm you have a good favicon. If you don't, then use their tool to create one (plus many other useful and related icons). Note: this requires that you have a good icon (e.g. PNG) to use as a basis for the favicon.
- Take advantage of http://realfavicongenerator.net suggestion to use
?v=version
option to help defeat the browser caching issue. This helped me. - Copy the favicon.ico to
public
andapp/assets/images
. You'll only need one but if you don't know which one, copying to both places doesn't hurt...or you can experiment to see which one works - take advantage of the?v=version
to perform your test. - Add the following line in the
<head></head>
section of your layouts in app/views/layouts files (e.g. application.html.erb):
<%= favicon_link_tag 'favicon.ico' %>
Hopefully that provides a simple recipe. I'm sure if I missed anything, someone can and will improve on this answer.
Solution 11 - Ruby on-Rails
I had a problem when I put file into /public/favicon.ico
, I am using AWS EBS.
I could fix the mistake.
The better solution for me was put the file into /app/assets/images/favicon.ico
and to use = favicon_link_tag 'favicon.ico'
Solution 12 - Ruby on-Rails
In rails 6, simply place it inside the public folder and reload the page.