当 config.assets.initialize_on_precompile 为 false 时设置常量
在 Heroku 上部署需要 application.rb 中的 config.assets.initialize_on_precompile=false
。从 Rails Guide 中,它说这“部分加载您的应用程序”。
所以我想知道设置在编译资产中使用的一些常量(例如 *.js.erb)的最佳实践是什么?将此标志设置为 false 后,我无法使用初始化程序中设置的任何内容。我不太清楚加载了应用程序的哪些部分,以及在执行编译步骤之前如何设置任何常量、变量等。
谢谢!
Deploying on Heroku requires that config.assets.initialize_on_precompile=false
in application.rb. From the Rails Guide it says that this "partially loads your application".
So I was wondering what the best practices were for setting some constants to be used in compiled assets (e.g. *.js.erb)? With this flag set to false I can't use anything that is set in initailizers. I'm not super clear on what parts of the application are loaded, and how I might be able to set any constants, variables etc before the compilation step is performed.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了类似的情况,幸运的是我能够轻松解决它。我知道
config.assets.initi....
正在 Heroku 上按照 Heroku 文档的要求执行。在此之前我只需要手动加载初始化程序文件。我只想要一个/config/initializers/settings.rb
文件,因此我添加了一个要求。这解决了问题。I ran into a similar situation, fortunately I was able fix it easily. I knew that the
config.assets.initi....
was being executed on heroku as heroku docs ask for it. I just had to manually load my initializer files before that. I wanted just one/config/initializers/settings.rb
file, so I added a require for that. And that fixed the problem.这是我能想到的最干净的解决方案
:
somejavascriptfile.js.erb
:config/initializers/facebook.rb
Here's the cleanest solution I could come up with
somejavascriptfile.js.erb
:config/initializers/facebook.rb
我对资产管道有点陌生,所以我不确定最佳实践。然而,我想到的一个解决方案是添加一个 rake 任务,该任务是 asset:precompile 的依赖项。我不太确定这些任务会做什么。我想象的实现不是很漂亮。
您可能必须创建模板文件,例如 *.js.erb.template,您的 rake 任务将其复制到 *.js.erb 文件,并对文件内容执行某种 gsub 以用常量值替换某些占位符。
希望其他人有更好的方法...
I'm somewhat new to the asset pipeline, so I'm not sure about best practices. However, one solution that comes to mind is to add a rake task that is a dependency of assets:precompile. I'm not quite sure what the taks would do though. The implementation I'm imagining isn't very pretty.
You'd probably have to create template files, like *.js.erb.template that your rake task would copy to *.js.erb files and perform some kind of gsub on the file contents to replace some placeholders with constant values.
Hopefully someone else has a better method...