mikeborgh
mikeborgh

Reputation: 1192

/favicon.ico Being Appended to Rails 4 Routes

This is a strange one. I started a new Rails 4 application today and then created a resource called transfer_functions. When I hit a url like localhost:3000/transfer_functions/1 it appends /favicon.ico to the end of it and I get this error in the server console.

ActionController::RoutingError (No route matches [GET] "/transfer_functions/1/favicon.ico"):

Any ideas? Thanks!

The app is using 'turbolinks' gem.

Update:

I'm using Twitter Bootstrap and created the application layout with the respective generator. I removed this line from my application.html.haml file and it fixed the problem.

%link(href="favicon.ico" rel="shortcut icon")

Not sure that I understand why that caused a problem.

Upvotes: 5

Views: 6387

Answers (3)

Pascal Lindelauf
Pascal Lindelauf

Reputation: 4870

We have regularly see 404's in our logs for requests for "old versions" of the favicon. On every new release (and thus on every new precompile), a new favicon file is generated with a unique hash. This seems to break in cases where someone has stored the favicon, e.g. by placing a shortcut to your site on his/her iOS home screen: the icon is lost and shows a broken icon symbol. So we're abandoning the favicon_link_tag/asset pipeline approach and reverting back to a favicon stored in the public folder by simply including:

<link href="favicon.ico">
<link href="favicon_ios.png" rel="apple-touch-icon" type="image/png">

Or in our case in HAML:

%link{:href => 'favicon.ico'}
%link{:href => 'favicon_ios.png', :rel => 'apple-touch-icon', :type => 'image/png'}

Upvotes: 0

sergserg
sergserg

Reputation: 22264

To use a favicon.ico file in your Rails 4 application, use the favicon_link_tag in your layout.

Typically in application.html.slim:

DOCTYPE html
html
  head
    = favicon_link_tag

You also need to have a file named favicon.ico in your app/assets/images folder.


Official documentation:

Returns a link loading a favicon file. You may specify a different file in the first argument. The helper accepts an additional options hash where you can override “rel” and “type”.

Options

:rel - Specify the relation of this link, defaults to 'shortcut icon'

:type - Override the auto-generated mime type, defaults to 'image/vnd.microsoft.icon'

Upvotes: 0

Mario Zigliotto
Mario Zigliotto

Reputation: 9025

The reason that error popped up was because the line you removed is essentially saying "look for a favicon.ico in the current directory". That haml code you posted renders into html that looks something like <link href="favicon.ico...".

The way you should handle a favicon is like this:

  1. Put your favicon file in this directory: your_webapp/app/assets/images/

  2. Edit your_webapp/app/views/layouts/application.html.hamland add the following under the document's head: = favicon_link_tag

Rails will use the asset pipeline to serve the favicon just like any other site asset. You just need the file name, and don't need to include the images folder within the url.

If you're interested in the docs: http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html#method-i-favicon_link_tag

You can also add in whatever you need to rel

Upvotes: 10

Related Questions