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 6647883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:35:25+00:00 2026-05-26T00:35:25+00:00

I have a Rails Engine gem in which I want to load a HomeController

  • 0

I have a Rails Engine gem in which I want to load a HomeController class definition dynamically from an initializer. I can instantiate the class correctly, but when I go to call the index action, I get this error :

TypeError in HomeController#index

can't convert nil into String
Rails.root: /home/chris/test_app

Full Trace:
actionpack (3.1.0) lib/action_view/template/resolver.rb:16:in `<<'
actionpack (3.1.0) lib/action_view/template/resolver.rb:16:in `build'
actionpack (3.1.0) lib/action_view/template/resolver.rb:127:in `find_templates'
actionpack (3.1.0) lib/action_view/template/resolver.rb:45:in `find_all'
actionpack (3.1.0) lib/action_view/template/resolver.rb:76:in `cached'
actionpack (3.1.0) lib/action_view/template/resolver.rb:44:in `find_all'
actionpack (3.1.0) lib/action_view/path_set.rb:21:in `find_all'
actionpack (3.1.0) lib/action_view/path_set.rb:20:in `each'
actionpack (3.1.0) lib/action_view/path_set.rb:20:in `find_all'
actionpack (3.1.0) lib/action_view/path_set.rb:19:in `each'
actionpack (3.1.0) lib/action_view/path_set.rb:19:in `find_all'
actionpack (3.1.0) lib/action_view/path_set.rb:29:in `exists?'
actionpack (3.1.0) lib/action_view/lookup_context.rb:94:in `template_exists?'

I cut the trace off after the actionpack part because it was really long, but I think this is all the relevant information.

Here is my Engine class definition:

module MyGem
   class Engine < Rails::Engine

      initializer 'my_gem.load_middleware' do |app|
        home_controller = create_controller 'HomeController'
      end

      def create_controller(class_name, &block)
        klass = Class.new ApplicationController, &block
        Object.const_set class_name, klass
        return klass
      end
   end
end

this is when i have the root path set to home#index . if I create a home_controller.rb in app/controllers in either the application or the gem like so:

class HomeController < ApplicationController
end

then everything works fine and the index action is rendered appropriately, so I’m sure there is no problem with my routes, views, or application controller.

Any light shed on this issue would be much appreciated.
edit
the output of

HomeController.view_paths.join " : "

is

/home/chris/gems/my_gem/app/views : /home/chris/test_app/app/views

  • 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-26T00:35:25+00:00Added an answer on May 26, 2026 at 12:35 am

    I’m not sure where you get the DSL “initializer” from… but that seems to cause a problem. It doesn’t get executed on new()

    This seems to work for me in Rails 3.0.7:

    module MyGem
       class Engine < Rails::Engine
    
         def initialize
            home_controller = create_controller 'HomeController'
         end
    
    # this doesn't seem to do anything...
    #
    #      initializer 'my_gem.load_middleware' do |app|
    #        home_controller = create_controller 'HomeController'
    #      end
    
          def create_controller(class_name, &block)
            klass = Class.new ApplicationController::Base , &block # shouldn't this be ApplicationController::Base ?
    
    #        Object.const_set class_name, klass     # module of superclass is ApplicationController, not Object
    
            ApplicationController.const_set(class_name, klass)  # name of the module containing superclass
            puts "Klass created! : #{Object.constants}"
            return klass
          end
       end
    end
    

    and running the code:

     h = MyGem::Engine.new
    Klass created! : [:Object, :Module, :Class, :Kernel, :NilClass, :NIL, :Data, :TrueClass, :TRUE, :FalseClass, :FALSE, :Encoding ... :BasicObject]
     => #<MyGem::Engine:0x00000006de9878> 
    
    
    > ApplicationController.const_get("HomeController")
     => ApplicationController::HomeController 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a engine style Rails plugin from which I can create a gem
I have a Rails Engine and I want to access the parents models. Is
I have an engine which defines some models and controllers. I want to be
I got application which runs on rails 2.3.11. I have created a separate gem
I have a Rails engine (someone else's gem) that I am trying to modify
I'm working on a small rails engine with I have turned into a gem.
The context : I have a rails engine (gem) that uses omniauth inside it
I have a users_manager engine which has a User model class. In an other
I have a Rails 3 engine gem that is for basic user authentication and
I have a rails application which is still showing the cachebusting numeric string at

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.