如何在 Rails 3.2中添加图标

我知道新的 Rails 应用程序会有一个空的 Favicon.ico 文件。我想知道我如何去添加一个图标。我知道您可以使用 favicon_link_tag助手,但是我不确定如何填充 Favicon.ico 文件。您使用图标生成器吗?如果是的话,哪一个最好?

我也想能够缓存它,轨道是否也自动这样做呢?

谢谢

54848 次浏览

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)

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.

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/

I highly recommend this option. It was easy to use and free http://converticon.com

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 = ""

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' %>

write in application.html.haml:

= favicon_link_tag '/images/favicon.ico'

place file favicon.ico in dir:

project/public/images

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.

The solution I found that worked for me was to do the following:

  1. 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.
  2. Take advantage of http://realfavicongenerator.net suggestion to use ?v=version option to help defeat the browser caching issue. This helped me.
  3. Copy the favicon.ico to 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.
  4. 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.

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 you Gemfile
    • 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.

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.

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'

In rails 6, simply place it inside the public folder and reload the page.