I am working on a cms, and have the need for dynamic template choice in the pages controller. I have a before filter that grabs the chosen template name in the user settings. Now I need to figure out how to render the correct layout using that instance variable.
Here is what I have so far:
#This sets @template to the template object. @template.name is "Default"
before_filter :get_template
layout "templates/#{@template.name.downcase.gsub(" ", "_")}"
#layout "templates/default" #This line renders fine
I’m getting the following error:
undefined method `name' for nil:NilClass
My guess is that the before_filter doesn’t necessarily run ‘before’ the template is called.
Is there a better way that I should be trying to accomplish this? I do not really have experience in using many templates and choosing which one to render.
Thanks in advance!
Try this: