ActionController::RoutingError (No route matches [GET] "/favicon.ico") in Rails - ruby-on-rails

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)
How to fix this error ?

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
Manually place the favicon.ico file in the public/ directory of your application.
Put the favicon.ico in app/assets/images/ and then change your <link ... tag to use image_path
<link href="<%= image_path("favicon.ico") %>" rel="shortcut icon" />
This will place the favicon.ico in public/assets/favicon.ico, not in the document root.
I suggest sticking with #1 above.
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.

This is what Rails generates in application.html.erb by default:
<%= favicon_link_tag 'favicon.ico', :rel => 'shortcut icon' %>
It doesn't find favicon.ico this way when it's under /public
It works correctly (finds favicon.ico under /public) if you change the tag to:
<%= favicon_link_tag %>

Putting favicon.ico in my public folder wasn't working, so I combined some of the other answers to come up with this simple working method.
Copy the output of favicon_link_tag and inject image_path like so:
<link href="<%= image_path("favicon.ico") %>" rel="shortcut icon" type="image/vnd.microsoft.icon" />
Now place favicon.ico in your assets/images folder and you're set.

Put the favicon.ico in app/assets/images/ and then add
<link href="<%= image_path("favicon.ico") %>" rel="shortcut icon" />
in the layout file.
This works for me.

Related

Internel URL for .ear file deployment [duplicate]

To working my static file (CSS, JS) I have to write absolute path like /AppName/templates/style/main.css. Is there any solution, that I could write relative path like style/main.css?
If your actual concern is the dynamicness of the webapp context (the "AppName" part), then just retrieve it dynamically by HttpServletRequest#getContextPath().
<head>
<link rel="stylesheet" href="${pageContext.request.contextPath}/templates/style/main.css" />
<script src="${pageContext.request.contextPath}/templates/js/main.js"></script>
<script>var base = "${pageContext.request.contextPath}";</script>
</head>
<body>
link
</body>
If you want to set a base path for all relative links so that you don't need to repeat ${pageContext.request.contextPath} in every relative link, use the <base> tag. Here's an example with help of JSTL functions.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%# taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
...
<head>
<c:set var="url">${pageContext.request.requestURL}</c:set>
<base href="${fn:substring(url, 0, fn:length(url) - fn:length(pageContext.request.requestURI))}${pageContext.request.contextPath}/" />
<link rel="stylesheet" href="templates/style/main.css" />
<script src="templates/js/main.js"></script>
<script>var base = document.getElementsByTagName("base")[0].href;</script>
</head>
<body>
link
</body>
This way every relative link (i.e. not starting with / or a scheme) will become relative to the <base>.
This is by the way not specifically related to Tomcat in any way. It's just related to HTTP/HTML basics. You would have the same problem in every other webserver.
See also:
Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
Is it recommended to use the <base> html tag?
Just use <c:url>-tag with an application context relative path.
When the value parameter starts with an /, then the tag will treat it as an application relative url, and will add the application-name to the url.
Example:
jsp:
<c:url value="/templates/style/main.css" var="mainCssUrl" />`
<link rel="stylesheet" href="${mainCssUrl}" />
...
<c:url value="/home" var="homeUrl" />`
home link
will become this html, with an domain relative url:
<link rel="stylesheet" href="/AppName/templates/style/main.css" />
...
home link
You start tomcat from some directory - which is the $cwd for tomcat. You can specify any path relative to this $cwd.
suppose you have
home
- tomcat
|_bin
- cssStore
|_file.css
And suppose you start tomcat from ~/tomcat, using the command "bin/startup.sh".
~/tomcat becomes the home directory ($cwd) for tomcat
You can access "../cssStore/file.css" from class files in your servlet now
Hope that helps, - M.S.
Instead using entire link we can make as below (solution concerns jsp files)
With JSTL we can make it like:
To link resource like css, js:
<link rel="stylesheet" href="${pageContext.request.contextPath}/style/sample.css" />
<script src="${pageContext.request.contextPath}/js/sample.js"></script>
To simply make a link:
<a id=".." class=".." href="${pageContext.request.contextPath}/jsp/sample.jsp">....</a>
It's worth to get familiar with tags
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
There is also jsp method to do it like below, but better way like above:
<link rel="stylesheet" href="<%=request.getContextPath()%>/style/sample.css" />
<script type="text/javascript" src="<%=request.getContextPath()%>/js/sample.js"></script>
To simply make a link:
<a id=".." class=".." href="<%=request.getContextPath()%>/jsp/sample.jsp">....</a>
This could be done simpler:
<base href="${pageContext.request.contextPath}/"/>
All URL will be formed without unnecessary domain:port but with application context.
This is a derivative of #Ralph suggestion that I've been using. Add the c:url to the top of your JSP.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:url value="/" var="root" />
Then just reference the root variable in your page:
<link rel="stylesheet" href="${root}templates/style/main.css">

How can I save 'favicon.ico' in radiant

I have not found a specific area to save the favicon.ico so I sending him to the area of assets.
But I can not get it back again.
This is in fact not related to Radiant at all.
As with any Rails application, you should just put your favicon.ico in the public/ folder.
Update: Ok.. if you múst use the backend to set the favicon, you could just do this in your layout:
<link rel="shortcut icon" href="<r:asset id="your asset's id"><r:link/></r:asset>" type="image/x-icon" />
.. or just 'hard code' the path to the asset you uploaded:
<link rel="shortcut icon" href="/assets/your asset's id/favicon.ico" type="image/x-icon" />

Rails and favicon issue

So I created a 16x16 favicon.ico file and placed it in my public/assets area. I also double downed and put it in my app/assets/image section.
I added and the image will not load up in Chrome, but it seems to load up in Firefox and Safari for me. That said, it won't load up in any of my friend's browsers. I also tried with or without the /.
<%= favicon_link_tag '/favicon.ico' %>
So I tried
<link rel="shortcut icon" href="cameronswiggett.com/favicon.ico" />
and same results.
I went to config/enviroments/production and made
config.serve_static_assets = true
the thing that confuses is me when I go to www.mysite.com/favicon.ico I see a broken image. So, obviously something is wrong but I am at a loss. Any suggestions?
Thanks!
Here is how I did it on my apps :
<link rel="shortcut icon" href="/img/favicon.png" type="image/png">
<link rel="icon" href="/img/favicon.png" type="image/png">
Here my favicon is in /public/img folder, it works for me so it may works for you, I added the two tags to make sure it works everywhere

How can I fix this error with my favicon?

In my rails app, I deleted the generic file favicon.ico and replaced it with the favicon specific to the app I'm working on, which has the name 1.ico. In the head section of my application layout file, I inserted the following code:
<link rel="shortcut icon" href="/1.ico" type="image/ico" />
When I open my application in my local environment, the favicon isn't visible, and I get the following response in my terminal:
Started GET "/public/favicon.ico" for 127.0.0.1 at 2012-07-16 13:33:22 -0400
ActionController::RoutingError (No route matches [GET] "/public/favicon.ico"):
I'm not sure how I can fix this error, so any help would be great! I realize that there are a fair number of questions related to favicons, but after doing research I didn't find any questions or answers that were of help. Thanks!
Try deleting the slash sign before 1.ico.
<link rel="shortcut icon" href="1.ico" type="image/ico" />
EDIT
Try using the Favicon Rails helper:
<%= favicon_link_tag '1.ico' %>
the path you pass to favicon_link_tag is probably incorrect. if your favicon is in the public directory, you want to pass '/path/from/public/favicon.ico', but if it's in the assets directory, you want to pass 'path/from/assets/favicon.ico'. note that there's no slash in the beginning

How can I arrange to have my favicon cached in MVC3?

I have placed a .ico and a .png file in the folder /Content/Ico and I have the following in my _layout.cshtml
<!-- Favicon -->
<link rel="shortcut icon" type="image/x-icon" href="/Content/Ico/favicon.ico">
<link rel="icon" type="image/png" href="/Content/Ico/tick-circle.png">
a) Is there some way for me to specify how long these favicons are cached for? Should I use some kind of web.config file inside the /Content folder?
b) Some of my code uses the syntax "<link href="#Url.Content("~/Content/ ... Should I be using #Url.Content? What's the difference between using that and just specifying /Content in the href ?
a) You could serve the favicon through a server side controller action in which you specify for how long it should be cached by decorating it with the [OutputCache] attribute:
[OutputCache(Duration = 3600, Location = OutputCacheLocation.Client)]
public ActionResult Favicon()
{
var icon = Server.MapPath("~/content/ico/favicon.ico");
return File(icon, "image/x-icon");
}
and then:
<link rel="shortcut icon" type="image/x-icon" href="#Url.Action("Favicon", "SomeController")" />
b) Always use #Url.Content("~/content/...") and never /content/... to specify relative paths to static files. The reason for that is that the Url.Content helper accounts for the virtual directory name and your site will continue to work even after you deploy it in a virtual directory in IIS. If you hardcode the url like this /content/... it will work locally but once you ship in IIS it will no longer work because now the correct location is /yourappname/content/... which is what the Url.Content helper takes into account.

Resources