Reputation: 1807
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
Upvotes: 77
Views: 56650
Reputation: 5871
In rails 6, simply place it inside the public folder and reload the page.
Upvotes: 0
Reputation: 341
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'
Upvotes: 0
Reputation: 1489
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 = ""
Upvotes: 116
Reputation: 41318
To generate a favicon for all platforms (not only for desktop browsers), you can use RealFaviconGenerator and the rails_real_favicon gem:
rails_real_favicon
gem to you Gemfile
favicon.json
. This file describes the icons you've just designed.rails generate favicon
to actually create the icons and HTML code.render 'favicon'
in your layouts to insert the HTML code in your pages.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.
Upvotes: 1
Reputation: 572
The solution I found that worked for me was to do the following:
?v=version
option to help defeat the browser caching issue. This helped me.public
and app/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.<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.
Upvotes: 0
Reputation: 33
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.
Upvotes: 1
Reputation: 13016
write in application.html.haml:
= favicon_link_tag '/images/favicon.ico'
place file favicon.ico in dir:
project/public/images
Upvotes: 4
Reputation: 19398
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)
Upvotes: 70
Reputation: 16888
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' %>
Upvotes: 9
Reputation: 410
I highly recommend this option. It was easy to use and free http://converticon.com
Upvotes: 5
Reputation: 381
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.
http://www.chami.com/html-kit/services/favicon/
Upvotes: 1
Reputation: 2351
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.
Upvotes: 2