Ruby-on-rails 如何在 Rails 3.2 中添加网站图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9901910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:08:18  来源:igfitidea点击:

How to add favicon in rails 3.2

ruby-on-railsfavicon

提问by noob

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_taghelper, but I am not sure how to populate the favicon.ico file. Do you use favicon generators? If so, which one is best?

我知道新的 rails 应用程序带有一个空的 favicon.ico 文件。我想知道如何添加收藏夹图标。我知道您可以使用favicon_link_tag帮助程序,但我不确定如何填充 favicon.ico 文件。你使用网站图标生成器吗?如果是这样,哪个最好?

I also want to be able to cache it, does rails do that automatically as well?

我也希望能够缓存它,rails 也能自动缓存吗?

Thanks

谢谢

采纳答案by gayavat

generate your favicon for example here: http://www.favicon.cc/and put in to public/ directory

例如在这里生成您的收藏夹图标:http: //www.favicon.cc/并放入 public/ 目录

UPDATEFavicon 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_tagto avoid favicon updating problems. I do not know browsers needed favicon in root. According to favicon wikiall modern browsers maintains

公共文件夹中的UPDATEFavicon 未预编译,它可能会被缓存很长时间。看起来最好使用favicon_link_tag来避免 favicon 更新问题。我不知道浏览器需要 root 中的 favicon。根据favicon wiki所有现代浏览器都维护

<link rel="shortcut icon" href="favicon path" /> (favicon_link_tag)

回答by Adrien Lamothe

Simply add this to the <head></head>section of your layouts:

只需将此添加到<head></head>您的布局部分:

<%= 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.

/app/assets/images/如果您正在使用资产管道,请将 favicon.ico 图像放入,否则放入/public/images/

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.

此外,如果将 Ruby 2.0 与 Rails 3.0.20(也可能是 3.0.x)一起使用,会出现一个错误,在尝试渲染 favicon.ico 时会引发异常。

The fix is to place the following code into application_controller.rb:

修复方法是将以下代码放入 application_controller.rb:

  config.relative_url_root = ""

回答by Tim Sullivan

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.

虽然所有这些答案都说要创建一个 16x16 图标,但实际情况是您应该同时创建 16x16 和 32x32,以支持视网膜显示。没有一个在线生成器在这方面做得很好。

On Mac, there is a great $5 app called Icon Slate, which allows you to easily create both formats in a single ICO file.

在 Mac 上,有一个很棒的 5 美元应用程序,称为Icon Slate,它允许您在单个 ICO 文件中轻松创建两种格式。

On Windows, I've used Axialis IconWorkshopwith great success, but it's a much heavier-duty tool, and is significantly more expensive at about 50.

在 Windows 上,我使用Axialis IconWorkshop取得了巨大的成功,但它是一个更重的工具,而且要贵得多,大约 50。

Both will create an ico file with both 16x16 and 32x32 images within it.

两者都将创建一个包含 16x16 和 32x32 图像的 ico 文件。

If you're using the asset pipeline, use the app/assets/images folder rather than /public. The number of fringe browsers that ignore the linktag is rapidly approaching zero, so jumping through hoops to accommodate them isn't worth it.

如果您使用资产管道,请使用 app/assets/images 文件夹而不是 /public。忽略link标签的边缘浏览器的数量正在迅速接近零,因此跳过箍以适应它们是不值得的。

As mentioned in other answers, use this in the headto display it:

正如其他答案中提到的,在 中使用head它来显示它:

<%= favicon_link_tag 'favicon.ico' %>

回答by MFrazier

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

我强烈推荐这个选项。它易于使用且免费 http://converticon.com

回答by shilovk

write in application.html.haml:

application.html.haml 中写入:

= favicon_link_tag '/images/favicon.ico'

place file favicon.icoin dir:

将文件favicon.ico放在目录中:

project/public/images

回答by fagiani

You pretty much need a 16x16 pixel image file called favicon.ico and it must be available publically in the root of your site.

您非常需要一个名为 favicon.ico 的 16x16 像素图像文件,并且它必须在您网站的根目录中公开可用。

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.

您始终可以使用主要的图像编辑器将您的徽标或其他图像转换为 .ico 格式。有像 Gimp 这样的免费选项可以比在线图标生成器更好地基于现有图像制作如此出色的图标。

回答by Matt

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.

我尝试了上面的链接,但这些服务使用起来不是很简单。我在另一个网站上找到了这个链接,它完美地复制了我的 .png 文件,使用起来非常简单。我想我也会分享这个链接,因为我认为这是一个更好的服务。

http://www.chami.com/html-kit/services/favicon/

http://www.chami.com/html-kit/services/favicon/

回答by marcpmichel

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.

多年没有这样做了,但 Gimp 能够保存具有不同大小的多个图像的 .ico 文件。您只需要导出为带有一些可见图层的 .ico 格式。

回答by philippe_b

To generate a favicon for all platforms (not only for desktop browsers), you can use RealFaviconGeneratorand the rails_real_favicon gem:

要为所有平台(不仅为桌面浏览器)生成图标,您可以使用RealFaviconGenerator和 rails_real_favicon gem:

  • Go to RealFaviconGeneratorand 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_favicongem to you Gemfile
    • Create a new file named favicon.json. This file describes the icons you've just designed.
    • Run rails generate faviconto actually create the icons and HTML code.
    • Add render 'favicon'in your layouts to insert the HTML code in your pages.
  • 转到RealFaviconGenerator并提交您的图片。您可以为每个平台制作图标、平台:iOS、Android 等。
  • 图标准备好后,单击“Rails”选项卡以获取在 Rails 项目中安装网站图标的步骤。基本上,您将被要求:
    • rails_real_favicon给你添加宝石Gemfile
    • 创建一个名为favicon.json. 该文件描述了您刚刚设计的图标。
    • 运行rails generate favicon以实际创建图标和 HTML 代码。
    • 添加render 'favicon'布局以在页面中插入 HTML 代码。

The advantage of this solution is that it injects the favicon files (favicon.ico, apple-touch-icon.png, but also browserconfig.xmland manifest.json) in the asset pipeline.

此解决方案的优点是它在资产管道中注入了图标文件(favicon.icoapple-touch-icon.pngbrowserconfig.xmlmanifest.json)。

Full disclosure: I'm the author of RealFaviconGenerator.

完全披露:我是 RealFaviconGenerator 的作者。

回答by hguzman

I had a problem when I put file into /public/favicon.ico, I am using AWS EBS.

将文件放入 时遇到问题/public/favicon.ico,我使用的是AWS EBS。

I could fix the mistake.

我可以改正错误。

The better solution for me was put the file into /app/assets/images/favicon.icoand to use = favicon_link_tag 'favicon.ico'

对我来说更好的解决方案是将文件放入/app/assets/images/favicon.ico并使用= favicon_link_tag 'favicon.ico'