How to set “shortcut icon” in rails? [duplicate]

2019-05-02 11:43发布

问题:

This question already has an answer here:

  • Adding icon to rails application 5 answers
<head>
  <title>Application</title>
  <% link { :rel => "shortcut icon", :href => "/images/favicon.ico" } %>
</head>

I can't see the image which i set, What's wrong with the above code? How can i run successfully?

回答1:

See doc:

<%= favicon_link_tag 'favicon.ico' %>


回答2:

favicon_link_tag(source='/favicon.ico', options={})

<%= favicon_link_tag %>

generates

<link href="/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />

You may specify a different file in the first argument:

<%= favicon_link_tag '/myicon.ico' %>

That’s passed to path_to_image as is, so it gives

<link href="/myicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />

The helper accepts an additional options hash where you can override “rel” and “type”.

For example, Mobile Safari looks for a different LINK tag, pointing to an image that will be used if you add the page to the home screen of an iPod Touch, iPhone, or iPad. The following call would generate such a tag:

<%= favicon_link_tag 'mb-icon.png', :rel => 'apple-touch-icon', :type => 'image/png' %>

Method Like

def favicon_link_tag(source='/favicon.ico', options={})
  tag('link', {
    :rel  => 'shortcut icon',
    :type => 'image/vnd.microsoft.icon',
    :href => path_to_image(source)
  }.merge(options.symbolize_keys))
end