iCyborg
iCyborg

Reputation: 4728

ActionController::RoutingError (No route matches [GET] "/favicon.ico") in 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 ?

Upvotes: 35

Views: 29397

Answers (4)

deefour
deefour

Reputation: 35360

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

  1. Manually place the favicon.ico file in the public/ directory of your application.
  2. 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.

Upvotes: 55

stevenup
stevenup

Reputation: 21

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.

Upvotes: 1

Abram
Abram

Reputation: 41884

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.

Upvotes: 1

Alex Vidmych
Alex Vidmych

Reputation: 91

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

Upvotes: 7

Related Questions