Question

How can I render javascript files containing ruby code (<%= ... %>) using Jammit? Jammit gem will always read JS files and will not evaluate the ruby code. Solutions? Thx!

Was it helpful?

Solution

I couldn't find a plugin so I wrote one: https://github.com/xpepermint/js_erb

OTHER TIPS

erb stands for "embedded ruby", and has nothing to do with html. There is nothing stopping you from making a js.erb view, with a route pointing to it. Unfortunately, you can't bundle it with jammit, since that would defeat the purpose of having it as a dynamic view.

One of the main use of an asset packager is for caching all the JS/CSS in one file. Even if Jammit or another packager was able to evaluate some Ruby code inside these files, it will be evaluated only once during the application startup, thus it would be totally useless!

It is not a great idea to put Ruby evaluated code in JS, there is always a good workaround.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top