Rails - Jammit render javascript with ruby code
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!
I couldn't find a plugin so I wrote one: https://github.com/xpepermint/js_erb
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.
精彩评论