Rails 中的 ActionController::RoutingError(没有路由匹配 [GET] “/favicon.ico") [英] ActionController::RoutingError (No route matches [GET] "/favicon.ico") in Rails
问题描述
我试过使用
<link href="/favicon.ico" rel="shortcut icon" />
还有这个
<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"):
favicon.ico 在 public 文件夹中(我也把它放在 app/assets 文件夹中)
the favicon.ico is there in public folder (I have also put it in app/assets folder too)
如何修复这个错误?
推荐答案
您收到此错误是因为您的 public/
中没有 favicon.ico
> 应用程序目录.由于该文件在那里不存在,Rails 继续前进,在 config/routes.rb
中寻找与 /favicon.ico
匹配的路由.
You're getting this error because you don't have a favicon.ico
in your public/
directory of your application. Because the file doesn't exist there, Rails moves on, looking for a route to match against /favicon.ico
in the config/routes.rb
.
您可以通过以下两种方式之一解决此问题
You can fix this in one of two ways
- 手动将
favicon.ico
文件放在应用程序的public/
目录中. 将
favicon.ico
放在app/assets/images/
中,然后更改您的标签使用
image_path
- Manually place the
favicon.ico
file in thepublic/
directory of your application. Put the
favicon.ico
inapp/assets/images/
and then change your<link ...
tag to useimage_path
<link href="<%= image_path("favicon.ico") %>" rel="shortcut icon" />
这会将 favicon.ico
放在 public/assets/favicon.ico
中,不 放在文档根目录中.
This will place the favicon.ico
in public/assets/favicon.ico
, not in the document root.
我建议坚持上面的#1.
至于为什么这个请求甚至出现在您的日志中,许多现代浏览器在域的根目录中查找 /favicon.ico
以用于书签,或选项卡或地址栏中的演示文稿.这就是为什么最好将 favicon.ico
保留在域的根目录中,以防浏览器决定(无论出于何种原因)忽略您的 <link rel="icon快捷方式" ...
标签.
As for why this request is even showing up in your logs, many modern browsers look in the root of the domain for /favicon.ico
to use for bookmarking, or presentation in a tab or the address bar. This is why it's a good idea to keep the favicon.ico
in the root of your domain, in case a browser decides (for whatever reason) to ignore your <link rel="icon shortcut" ...
tag.
这篇关于Rails 中的 ActionController::RoutingError(没有路由匹配 [GET] “/favicon.ico")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!