简体   繁体   中英

link CSS file to Haml layout Template

I am trying to link css file to haml layout template

I have method in ApplicationHelper to generate proper html

module ApplicationHelper

  def styletag(sheet_name)
    "<link rel='stylesheet' href='/assets/stylesheets/#{sheet_name}.css'>"
  end

end

and also link to layout template

!!! 5
%html
  %head
    %title Rotten Potatoes!
    = stylesheet_link_tag 'application'
    = styletag 'default'
    = javascript_include_tag 'application'
    = csrf_meta_tags
  %body
    = yield

but it seems like haml doesn't consider it like a tag and it's displayed like a text.

You need to add html_safe. Rails by default is escaping the html in your text so you need to tell rails that it should not do so.

See http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/stylesheet_link_tag and click "Show Source" If you change it to this:

def styletag(sheet_name)
    "<link rel='stylesheet' href='/assets/stylesheets/#{sheet_name}.css'>".html_safe
end

it should work for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM