Load ruby on rails template from database
I wonder if there is a way for me to store ruby on rails vi开发者_Go百科ew files into database store and have that fetched directly from there. The reason is I want to create a CMS with all the user data stored in database, so I would like to have the template stored in database but still retain the whole ActionView mechanism.
If there are only a few cases, you can simple use render:
template = UserTemplate.find(<find criteria>)
render :inline => template.text
(Assuming that UserTemplate
is the class modeling the templates)
Note, however, that I would primarily use database templates for dynamically modified templates. There are certainly uses for it -- at Rhiza we allow for customers to modify their welcome section dynamically, which would require a dynamic template.
Solution for your problem is the Template Engine in Rails .
Liquid template engine is an extraction from the e-commerce system Shopify. Shopify powers many thousands of e-commerce stores which all call for unique designs.
MasterView : Alternative Template Engine in Rails .
Choose the one which suits your requirement .
Hope this helps !
Creating a custom template resolver is the way to go. Check this. You need a custom resolver that instead of fetching from the filesystem, fetches from the database.
精彩评论