How to fix favicon error in Rails

RubyPlus uses Amazon Cloudfront as the CDN. All the favicons for different devices and sizes are handled by using:

favicon_link_tag 

I have observed some requests get the error:

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

in the log files. In the page source, the favicon helper generates:

<link rel="shortcut icon" type="image/x-icon" href="/assets/favicon-b742c2941.ico" />

The signature changes if I make any modification to the assets. The users who have bookmarked before I used the CDN are still looking for the old favicon. Since it is no longer available, it crashes. This is bad for user experience. Add a favicon method in welcome controller.

def favicon
  send_file 'public/favicon.ico', type: 'image/x-icon', disposition: 'inline'
end

This method renders the favicon image inline. Define the route in config/routes.rb:

get 'favicon', to: "welcome#favicon"

Make sure you have favicon.ico in public/images folder. Now if you click the favicon, you will see the favicon rendered on the browser.

Reference

Render image with Rails controller


Related Articles


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.