Technology and Software

Favicon on Rails

Did you ever wanted to generate a <link rel="shortcut icon" href="/favicon.ico" /> tag with an helper to take advange of Rails’asset hosts, that is <link rel="shortcut icon" href="http://static.yourserver.com/favicon.ico" /> ? You’re only three easy steps away from it.

1. Add this code to lib/asset_tag_helper_ext.rb

module ActionView
  module Helpers
    module AssetTagHelper
      def link_tag(url_options = {}, tag_options = {})
        href = url_options.is_a?(Hash) ?
          url_for(url_options.merge(
            :only_path => false)) : url_options
        tag(
          "link",
          "rel"   => tag_options[:rel] || nil,
          "type"  => tag_options[:type] || nil,
          "title" => tag_options[:title] || nil,
          "href"  => compute_public_path(href, "", "")
        )
      end
    end
  end
end

2. Require it at the end of config/environment.rb as

require "asset_tag_helper_ext"

3. Add <%= link_tag("/favicon.ico", :rel => "shortcut icon") %> to your layout. Done!

This code has been tested with Rails 2.0.2 and Rails 2.3.2.

Advertisements
Standard

8 thoughts on “Favicon on Rails

  1. James West says:

    Hi,
    I have not come accross asset hosts yet being a rails newbie but from your code it looks like a helper to me and was wondering why this file is not in the helpers folder?

  2. Hi James! I wanted to create something like stylesheet_link_tag and I needed to use compute_public_path to do it. That method is defined as private into actionpack-2.x.x/lib/action_view/helpers/asset_tag_helper.rb so I had to use those nested module definitions to add link_tag into the AssetTagHelper module.
    Furthermore having a small lib file makes it easier to add it into new projects.

  3. fredzirdung says:

    Hi Paolo,

    Have you tried this with rails 2.3.x ? It’s not working for me – I’m getting a strange error on another line in my template:

    ActionView::TemplateError (undefined method `content_for’ for #)

    Any ideas?

    Thanks,
    Fred

  4. Paolo says:

    Hi Fred! I’m using Rails 2.3.2 in my apps right now and that piece of code keeps working.

    content_for should be defined in any view. Could you post the complete trace for your error so I can understand if there is some bug or it’s a configuration issue? Thanks

  5. fredzirdung says:

    The trace doesn’t give too much info about the probem, but it’s included below.

    All I’ve done is copy the code above, place it in a file under lib, and included it in my env, per steps #1 & #2. I haven’t even made the call to link_tag in my view!

    Thanks for your help.

    —- environment.rb:

    require ‘lib/asset_tag_helper_ext’

    —- trace:

    Processing ProductsController#index (for 127.0.0.1 at 2009-06-29 15:07:00) [GET]
    Rendering template within layouts/application
    Rendering products/index

    ActionView::TemplateError (undefined method `content_for’ for #) on line #1 of app/views/products/index.html.erb:

    1:
    2:
    3:
    4:

    app/views/products/index.html.erb:1

    Rendered rescues/_trace (90.2ms)

    Processing ApplicationController#index (for 127.0.0.1 at 2009-06-29 15:07:01) [GET]

    ActionView::TemplateError (undefined method `debug’ for #) in /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/templates/rescues/_request_and_response.erb:

    Rendered rescues/_trace (95.8ms)
    /!\ FAILSAFE /!\ Mon Jun 29 15:07:01 -0700 2009
    Status: 500 Internal Server Error

    ActionView::TemplateError (undefined method `debug’ for #) in /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/templates/rescues/_request_and_response.erb:

    In /opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/templates/rescues/_request_and_response.erb

  6. Hello,

    thanks for this helpful helper.
    Instead of calling the module from environment.rb, I simply created a helpers/asset_tag_helper.rb file and put the matching module code (module AssetTagHelper…) in it. It works like a charm!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s