如标题中所述,我要防止缓存特定资产,即javascript文件something.js.erb.情况如下:
As stated in the title I want to prevent caching of a specific asset, namely a javascript file something.js.erb. The situation is like as follows:
something.js.erb的内容:
... var something = <%= SomethingHelper.get_something.to_json %>; ...它成功绑定了SomethingHelper中的值,但仅绑定了一次,除非手动编辑了javascript文件,否则就不会再分配var something的值.
It binds the value from SomethingHelper successfully but only once and unless the javascript file is edited by hand the value of var something is never assigned again.
这可能会以某种方式出现,但显然不能满足我的需求. SomethingHelper.get_something的输出根据通话时间而变化.因此,我需要在编译的something.js文件中查看最新数据.
This might be somehow expected but clearly doesn't meet my needs. Output of SomethingHelper.get_something changes according to call time. So I need to see up-to-date data in my compiled something.js file.
我的确切需求:
- 我不想整体禁用资产管道缓存
- 我只希望每次请求something.js.erb时都将其渲染.
- I don't want to disable asset pipeline caching as a whole
- I only want something.js.erb to be rendered every time it is requested.
这甚至有可能吗?
环境信息:
- 路轨4
- 开发模式
- Rails自己的服务器,但将在prod上的nginx上
谢谢
推荐答案您正在将前端业务逻辑与数据结合起来.这是不可取的,这也是我在大多数情况下不使用或不建议使用ERB + JS的原因之一(尤其是像Rails教程和指南一样急于触发响应行为).你也更好...
You're marrying front-end business logic with data. This is inadvisable, and one of the reasons I don't use or recommend using ERB + JS for most scenarios (especially triggering behavior on response like Rails tutorials and guides are keen on doing). You are better off either…
例如:
# /app/views/layouts/application.html.erb <%= tag :meta, name: 'something', content: @something %> # /app/assets/javascripts/application.js $('meta[name="something"]').attr('content');更多推荐
Rails资产管道:如何防止缓存特定资产
发布评论