【问题标题】:Rails precompile index.html.erbRails 预编译 index.html.erb
【发布时间】:2019-08-21 16:42:15
【问题描述】:

我有包含<%= 'Hello world!' %>app/assets/index.html.erb,并将require "sprockets/railtie" 添加到config/application.rb,但是当我运行rake assets:precompile (RAILS_ENV=development) public/assets 时仍为空。 我错过了什么? 我也在使用webpacker,最初没有使用sprockets

【问题讨论】:

  • 你对 assets:precompile 之后的 public/assets 有什么期望?
  • 我希望 index.html 包括。 “你好世界!”。我有一个错字:模板在app/assets/index.html.erb

标签: ruby-on-rails ruby asset-pipeline erb sprockets


【解决方案1】:

视图不是资产。每次 rails 处理 HTTP 请求时,您的视图模板(erb、slim 等)都会被编译和渲染。阅读更多关于 Rails 视图的信息here

Assets 仅包含 css、js、字体、图像等。如果您在 assets 文件夹中没有任何 assets 文件,那么“rake assets:precompile”将什么也不做。更多关于资产管道here.

UPD. May be this solution helps for you.

UPD2.

  1. 我用 创建了文件assets/html/index.html.erb
  2. 我在config/application.rb 中添加了关注行:
config.assets.precompile = ['*.js', '*.css', '*.html.erb']
  1. 我在 manifest.js 中添加了后续行
//= link_directory ../html .html
  1. 我运行rake assets:precompile 并查看:
...
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. 我打开编译文件并看到“Hello, world”

【讨论】:

  • 我不认为这是正确的。 Sprockets 也应该预编译 erb。我不需要动态渲染所有视图 - 只需要那些发生变化的视图。
  • @Jasper 是的,sprockets 可以预编译 erb,但这个 erb 不用于视图。您可以在 css 中使用 erb:guides.rubyonrails.org/asset_pipeline.html#css-and-erb 或在 js 中使用:guides.rubyonrails.org/…。如果您找到将 html 视图编译为静态文件的方法 - 请告诉我 :)
  • 你应该可以配置任何你想要的预编译,我只是不知道,如何。
  • 遗憾的是,这对我不起作用。我很确定这是因为我最初删除了 sprockets 并手动添加它,现在缺少一些重要的配置。您在代码中还引用了manifest.js 的什么地方?
  • @Jasper Manifest 需要 sprocket 4 的 js。更多:github.com/rails/sprockets/blob/master/UPGRADING.md#manifestjs
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 2013-09-27
  • 2013-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多