如何在rails 3.2中添加favicon

noo*_*oob 74 favicon ruby-on-rails

我知道新的rails应用程序带有一个空的favicon.ico文件.我想知道如何添加一个favicon.我知道你可以使用favicon_link_tag帮助器,但我不知道如何填充favicon.ico文件.你使用favicon发电机吗?如果是这样,哪一个最好?

我也希望能够缓存它,rails也会自动执行此操作吗?

谢谢

Adr*_*the 114

只需将其添加到<head></head>布局部分:

<%= favicon_link_tag 'favicon.ico' %>
Run Code Online (Sandbox Code Playgroud)

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

此外,如果使用带有Rails 3.0.20(也可能是3.0.x)的Ruby 2.0,则会出现错误,这会在尝试渲染favicon.ico时引发异常.

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

  config.relative_url_root = ""
Run Code Online (Sandbox Code Playgroud)


gay*_*vat 69

例如,在这里生成您的favicon:http://www.favicon.cc/ 并放入public /目录

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

<link rel="shortcut icon" href="favicon path" /> (favicon_link_tag)
Run Code Online (Sandbox Code Playgroud)

  • 那么favicon不会进入资产管道? (26认同)
  • 在http://yourdomain.com/favicon.ico上使用favicon更好/标准,因为有些浏览器会直接请求它们(即使你没有使用meta标签). (4认同)

Tim*_*van 9

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.

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

如其他答案中所述,head在显示它时使用它:

<%= favicon_link_tag 'favicon.ico' %>
Run Code Online (Sandbox Code Playgroud)

  • [realfavicongenerator.net](http://realfavicongenerator.net)是免费的,很棒!不要浪费你的钱;) (4认同)
  • 投资精湛的工具永远不会浪费你的钱.虽然该网站比大多数网站更好,但它甚至不能与像IconWorkshop这样的工具相比,也不能与Icon Slate的愉快用户体验相媲美,后者也可以离线使用. (3认同)

MFr*_*ier 5

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