Skip to content

Instantly share code, notes, and snippets.

@FrankFang
Forked from Dagnan/application.html.erb
Created July 17, 2017 08:56
Show Gist options
  • Save FrankFang/e17076795e1001140a9c1020f783d1f7 to your computer and use it in GitHub Desktop.
Save FrankFang/e17076795e1001140a9c1020f783d1f7 to your computer and use it in GitHub Desktop.
Inline CSS or JS in Rails 5
<!DOCTYPE html>
<html>
<head>
<%= inline_js 'application.js' %>
<%= inline_css 'application.css' %>
</head>
<body>
</body>
</html>
module AssetsHelper
def inline_file(path)
if assets = Rails.application.assets
asset = assets.find_asset(path)
return '' unless asset
asset.source
else
File.read(File.join(Rails.root, 'public', asset_path(path)))
end
end
def inline_js(path)
"<script>#{inline_file path}</script>".html_safe
end
def inline_css(path)
"<style>#{inline_file path}</style>".html_safe
end
end
@FrankFang
Copy link
Author

Thanks. But where is assets_helper.rb?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment