2

I have app/assets/index.html.erb containing <%= 'Hello world!' %>, and added require "sprockets/railtie" to config/application.rb but when I run rake assets:precompile (RAILS_ENV=development) public/assets stays empty. What am I missing? I am also using webpacker and did not use sprockets initially.

Jasper
  • 1,971
  • 19
  • 34

1 Answers1

3

Views are not assets. Your view templates (erb, slim and etc.) will be compiled and rendered each time when rails process HTTP request. Read more about Rails views here.

Assets includes only css, js, fonts, images and etc. If you have no any assets files in assets folder then "rake assets:precompile" will do nothing. More about assets pipeline here.

UPD. May be this solution helps for you.

UPD2.

  1. I create file assets/html/index.html.erb with <%= "Hello, world!"%>.
  2. I add follow line to my config/application.rb:
config.assets.precompile = ['*.js', '*.css', '*.html.erb']
  1. I add follow line to my manifest.js
//= link_directory ../html .html
  1. I run rake assets:precompile and see:
...
I, [2019-03-31T13:56:28.979563 #50803]  INFO -- : Writing rails_app/public/assets/index-f4e7c3b6ac0beff671efa8cf84639011c06e58ca53a78d83f36107316cec125f.html
I, [2019-03-31T13:56:28.979832 #50803]  INFO -- : Writing rails_app/public/assets/index-f4e7c3b6ac0beff671efa8cf84639011c06e58ca53a78d83f36107316cec125f.html.gz
...
  1. I open compiled file and see "Hello, world"
Matilda Smeds
  • 1,384
  • 11
  • 18
QNester
  • 463
  • 4
  • 9
  • I do not think that is correct. Sprockets should precompile erb too. I do not need to render all views dynamically - only those who change. – Jasper Mar 31 '19 at 10:15
  • @Jasper Yes, sprockets can precomplie erb, but this erb is not for views. You can use erb in css: https://guides.rubyonrails.org/asset_pipeline.html#css-and-erb or in js: https://guides.rubyonrails.org/asset_pipeline.html#javascript-coffeescript-and-erb . If you find way to compile html views to static files - give me know pls :) – QNester Mar 31 '19 at 10:25
  • 1
    You should be able to configure precompiling whatever you want, I just don't know, how. – Jasper Mar 31 '19 at 10:47
  • sadly that is not working for me. I am pretty sure that is resulting from me having removed `sprockets` initially and added it manually and am now missing some important config. Where else do you reference `manifest.js` in your code? – Jasper Mar 31 '19 at 19:34
  • 1
    @Jasper Manifest js required for sprocket 4. More: https://github.com/rails/sprockets/blob/master/UPGRADING.md#manifestjs – QNester Mar 31 '19 at 20:06