Search code examples
rubyfaviconcamping

Favicon not showing with camping


I want to learn about webapps. I decided to learn by doing and chose to start simple with Camping as (i). it is small & (ii). i know some ruby.

The favicon is not showing. I am using a favicon taken from another site so it know its file format is valid.

Here is the code from the controller.

  class Favicon < R '/favicon\.ico'
    # Load the favicon into memory
    FAVICON = File.read('favicon.ico')
    def get
      @headers['Content-Type'] = "image/x-icon"
      FAVICON
    end
  end

Here is the code from the view: I purposely placed a link to the favicon twice as an experiment. No joy.

def layout

html do 
  head do
    title 'Custom Made Kameez'
    link :rel => 'icon', :href => 'favicon.ico', :type => 'image/x-icon' 
    link :rel => 'shortcut icon', :href => 'favicon.ico', :type => 'image/x-icon'
    link :rel => 'stylesheet', :type => 'text/css', :href => '/styles.css', :media => 'screen'        
  end

I have tried clearing my cache and using Firefox and IE, same issue.


Solution

  • Two problems as far as I can see.

    You should use an absolute path to the favicon:

    link :rel => 'icon', :href => '/favicon.ico', :type => 'image/x-icon'
    

    You should read the file as binary:

    FAVICON = File.binread('favicon.ico')