Does anyone know how to render a partial (or at least include another file) within a asset file in rails? I have a templates/ folder setup (which contains static HTML files) and there bits and pieces within these files that I would like to abstract and share between other template files.
The render method doesn’t work in any asset pipeline files. I’m sure I can include it somehow by finding the correct module, but I’m not sure if this would work since the render module itself may rely on other modules, methods, variables.
Any ideas on how to do this? Or if its possible?
I’ve come up with my own solution.
You create a helper file in the lib folder and call it
assets_helper.rb. The code within the asset_helper is as follows:Then you include that file at the top of each asset file that you wish to use it in. Inside of the file you can use the partial command like so.
This works. But the only issue is that the Rails 3.1 will cache anything that’s in the assets folder. So if you edit a partial you will need to update the root file that it’s inside of so that the cache gets cleared for that file.