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

  • SEARCH
  • Home
  • 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 4558112
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:36:11+00:00 2026-05-21T17:36:11+00:00

When you want to override a generator template (without replacing the generator itself), in

  • 0

When you want to override a generator template (without replacing the generator itself), in Rails 3 you can just drop files in appropriately named places in lib/templates and Rails will find them.

What if you want to do this with a gem? I’m trying to take my team’s standardized scaffold format and gemify it so we can share it in all projects and update it easily, rather than copy files into lib/ in every project. This works fine for the cases where I’ve created a new generator; I hook into it with config.generators in application.rb and Rails finds it. But when I drop template files into lib/templates in the gem, Rails finds its own default templates first, and renders them instead of mine. I think the search order is RAILS_ROOT/lib/templates, RAILS_GEMS/lib/templates, OTHER_GEMS/lib/templates.

What’s the solution? I’m not finding much docco on this, and code-diving through Rails hasn’t presented an obvious solution. Thanks!

  • 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-21T17:36:12+00:00Added an answer on May 21, 2026 at 5:36 pm

    We figured this out. the generators config has a ‘templates’ variable that lists search paths for templates. The problem is indeed that it searches this array in order until it finds a match, so templates in your app or in Rails will get found before templates in your gem.

    The solution is to have your gem’s Railtie put the templates path onto the beginning of the array of template paths. It looks like this. This file is in [GEM]/lib/my_gem.rb. The templates are parallel to it in [GEM]/lib/templates/.

    module MyGem
      class Railtie < Rails::Railtie
        config.generators do |g|
          g.templates.unshift File::expand_path('../templates', __FILE__)
        end 
      end
    end 
    

    If the templates have a path inside [GEM]/lib/templates that matches the path of the default template you are overriding, this should work. For example, if you’ve done this and you create [GEM]/lib/templates/active_record/model/model.rb, it will override the default AR model template.

    No monkeypatching of the generators required.

    EDIT: Note that since this answer was originally posted, “config.generators” has been removed from Rails. Use config.app_generators instead as per pixelearth’s answer below.

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

Sidebar

Related Questions

I want to override the JSON MIME type (application/json) in Rails to (text/x-json). I
I am creating a blog application on Rails 3, and I want to override
I want to override OnMouseClick and OnMouseDoubleClick and perform different actions depending on which
I want to override the bad default tabbing scheme in emacs so that it
I want to override access to one variable in a class, but return all
I have a function, a() , that I want to override, but also have
I want to create a Java application bundle for Mac without using Mac. According
I want to use a custom sequence generator in my application, but the entity
Is it possible to override Tomcat's embedded generator of JSESSIONID, to be able to
I want to implement an aplication where I have various Objects that can be

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.