Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add favicon in rails 3.2

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

like image 351
noob Avatar asked Mar 28 '12 06:03

noob


2 Answers

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 = "" 
like image 108
Adrien Lamothe Avatar answered Sep 22 '22 10:09

Adrien Lamothe


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) 
like image 20
gayavat Avatar answered Sep 20 '22 10:09

gayavat