Ruby-on-rails Rails 中的 ActionController::RoutingError(没有路由匹配 [GET] "/favicon.ico")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15687506/
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
ActionController::RoutingError (No route matches [GET] "/favicon.ico") in Rails
提问by iCyborg
I have tried to use
我试过用
<link href="/favicon.ico" rel="shortcut icon" />
as well as this
还有这个
<link href="/assets/favicon.ico" rel="shortcut icon" />
but I am still seeing this error in the log file
但我仍然在日志文件中看到这个错误
ActionController::RoutingError (No route matches [GET] "/favicon.ico"):
the favicon.ico is there in public folder (I have also put it in app/assets folder too)
favicon.ico 在 public 文件夹中(我也把它放在 app/assets 文件夹中)
How to fix this error ?
如何修复此错误?
回答by deefour
You're getting this error because you don't have a favicon.icoin your public/directory of your application. Because the file doesn't exist there, Rails moves on, looking for a route to match against /favicon.icoin the config/routes.rb.
因为你没有你得到这个错误favicon.ico在你的public/应用程序的目录。由于该文件在那里不存在,Rails 继续前进,/favicon.ico在config/routes.rb.
You can fix this in one of two ways
您可以通过以下两种方式之一解决此问题
- Manually place the
favicon.icofile in thepublic/directory of your application. Put the
favicon.icoinapp/assets/images/and then change your<link ...tag to useimage_path<link href="<%= image_path("favicon.ico") %>" rel="shortcut icon" />This will place the
favicon.icoinpublic/assets/favicon.ico, notin the document root.
- 手动将
favicon.ico文件放在public/应用程序的目录中。 把
favicon.ico中app/assets/images/,然后改变你的<link ...标签使用image_path<link href="<%= image_path("favicon.ico") %>" rel="shortcut icon" />这会将
favicon.icoin放置在文档根目录中public/assets/favicon.ico,而不是放在文档根目录中。
I suggest sticking with #1above.
我建议坚持上面的#1。
As for whythis request is even showing up in your logs, many modern browsers look in the root of the domain for /favicon.icoto use for bookmarking, or presentation in a tab or the address bar. This is why it's a good idea to keep the favicon.icoin the root of your domain, in case a browser decides (for whatever reason) to ignore your <link rel="icon shortcut" ...tag.
至于为什么这个请求甚至出现在您的日志中,许多现代浏览器会在域的根目录中查找/favicon.ico以用于书签或选项卡或地址栏中的显示。这就是为什么最好将 保留favicon.ico在域的根目录中,以防浏览器决定(出于任何原因)忽略您的<link rel="icon shortcut" ...标签。
回答by Alex Vidmych
This is what Rails generates in application.html.erb by default:
这是 Rails 默认在 application.html.erb 中生成的内容:
<%= favicon_link_tag 'favicon.ico', :rel => 'shortcut icon' %>
It doesn't find favicon.ico this way when it's under /public
当它位于 /public 下时,它不会以这种方式找到 favicon.ico
It works correctly (finds favicon.ico under /public) if you change the tag to:
如果您将标签更改为:
<%= favicon_link_tag %>
回答by Abram
Putting favicon.icoin my public folder wasn't working, so I combined some of the other answers to come up with this simple working method.
把favicon.ico我的公用文件夹是行不通的,所以我结合一些其他的答案拿出这个简单的工作方法。
Copy the output of favicon_link_tagand inject image_pathlike so:
像这样复制favicon_link_tag和注入的输出image_path:
<link href="<%= image_path("favicon.ico") %>" rel="shortcut icon" type="image/vnd.microsoft.icon" />
Now place favicon.icoin your assets/imagesfolder and you're set.
现在放在favicon.ico你的assets/images文件夹中,你就设置好了。
回答by stevenup
Put the favicon.icoin app/assets/images/and then add
将favicon.ico放在app/assets/images/ 中,然后添加
<link href="<%= image_path("favicon.ico") %>" rel="shortcut icon" />
in the layout file.
在布局文件中。
This works for me.
这对我有用。

