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:
Content of something.js.erb
:
...
var something = <%= SomethingHelper.get_something.to_json %>;
...
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.
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.
My exact need:
- 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.
is this even possible?
Environment info:
- Rails 4
- Development mode
- Rails' own server but will be on nginx on prod
Thanks
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…
meta
tag in your layout with the relevant information.For example:
I can suggest 2 options:
1)Use inline js to set variable:
2)Store the variable in your html and call it from your js: