-
-
Save JayTeeSF/82bf726e0e86e6f76b41e8cd563a11d9 to your computer and use it in GitHub Desktop.
Inline CSS or JS in Rails 5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<%= inline_js 'application.js' %> | |
<%= inline_css 'application.css' %> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment