Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7503409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:12:49+00:00 2026-05-29T21:12:49+00:00

I’m building a Rails 3 app where users can select one of a number

  • 0

I’m building a Rails 3 app where users can select one of a number of templates and build a little website. I’m trying to initialize all of the available templates when the application starts. That’s essentially a file that let’s me define them all, then calls Template.add(template) for each one of them, which in turn stores them in @@templates and I can access them via Template.find(name).

The problem is that in development mode, the initializer I have making the calls to Template.add are getting loaded on the first request, then wiped out on reload!. I’ve read about config.to_prepare, but it isn’t working for me, likely because I’m requiring the templates like this:

# template config at app/templates/template_name/template_name.rb
path = File.expand_path(Rails.root + 'app/templates')
Dir[File.join(path, '/*')].each do |template|
  name = File.basename(template)
  require File.join(path, name, "#{name}.rb")
end

What can I do to either reload these files after reload! or keep them from getting trashed at all? Also, if you have a recommendation for a better way to handle this, I’m all ears. I’m still getting my footing with Rails, especially in regards to configuration stuff.

I’m temporarily avoiding this problem by loading the data in the class. Save me from this ugly, non-modular nastiness.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T21:12:50+00:00Added an answer on May 29, 2026 at 9:12 pm

    I think you ought to be able to do this with config.to_prepare if you move your calls to Template.add out of the template_name.rb files (I am assume that is where they are now), and into your loading code, just after the require. For example, consider adding this class method to your Template class, and then maybe even calling it from the class itself when it first gets loaded:

    class Template
      def self.load_all
        # template config at app/templates/template_name/template_name.rb
        path = File.expand_path(Rails.root + 'app/templates')
        Dir[File.join(path, '/*')].each do |template|
          name = File.basename(template)
          require File.join(path, name, "#{name}.rb")
    
          # Register the template even if the class itself is already loaded.
          Template.add(name)
        end
      end
    
      # Initially load all templates.
      Template.load_all
    end
    

    Then remove the Template.add(name) code from the template_name.rb files where they are registering themselves. (Again, I am assuming this is where that currently happens – I am not quite clear on this from your original post.) Or perhaps you just want to make sure Template.add is idempotent and silently discards duplicates.

    Lastly, add the following to config/environments/development.rb:

    config.to_prepare do
      Template.load_all
    end
    

    That will reload all your templates after the Template class reloads.

    I haven’t tried this, but with the call to Template.load_all I suggested at the end of the Template class, you may not even need the config.to_prepare part.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm trying to select an H1 element which is the second-child in its group
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.