Routing Error uninitialized constant TestController

AI-generated summary

User Sudaraka reported a Routing Error: uninitialized constant TestController while developing a Discourse plugin intended to serve JSON data at /admin/plugins/test/list. Initially, the user attempted to structure the plugin using a Rails engine (Test::Engine) and a namespaced controller (Test::TestController), but the routing configuration failed to resolve the constant, resulting in the error. After seeking assistance, Sudaraka resolved the issue by removing the engine wrapper entirely. The solution involved defining the controller as a top-level class (::TestController) inheriting from ApplicationController and simplifying the route definitions within the after_initialize block. This adjustment allowed the route /admin/plugins/test/list to correctly map to the test#list action, successfully returning the expected JSON response.

Hi i wrote a simple plugin just for education purpose. I need to get a json by calling http://localhost:4000/admin/plugins/test/list
url. I wrote the plugin.rb as

# name: test-plugin
# about: This is intended to be a test
# version: 1.0.0
# authors: Test_Author

    after_initialize do
    	module ::Test
    	    class Engine < ::Rails::Engine
    	      engine_name 'test'
    	      isolate_namespace Test
    	    end
      	end
      	require_dependency 'application_controller'
      
     	class ::Test::TestController < ::ApplicationController
    	  	#This class will contain all the back end controller methods 
    	    requires_plugin 'test-plugin'
    	    require_dependency 'admin_constraint'
    	    #this will return the JSON of users in the plugin store 
    	    def list
        		render json: { name: "donut", description: "delicious!" }
    	    end
    	end

    	Test::Engine.routes.draw do
    		get "/list" => "test#list", constraints: AdminConstraint.new
    	end

    	Discourse::Application.routes.prepend do
       		mount ::Test::Engine, at: "/test"
    	end

      	add_admin_route "test.title", "test"

      	Discourse::Application.routes.append do
       		get "/admin/plugins/test" => "admin/plugins#index"
       		get "/admin/plugins/test/list" => "test#list"
    	end
end

But when i call the url , it always return

Routing Error
uninitialized constant TestController

Rails.root: /vagrant

Application Trace | Framework Trace | Full Trace
config/initializers/100-quiet_logger.rb:17:in `call_with_quiet_assets'
config/initializers/100-silence_logger.rb:29:in `call'
lib/middleware/missing_avatars.rb:21:in `call'
lib/middleware/turbo_dev.rb:33:in `call'

can someone please help. I’m just a newbie here.

Thank you all. I removed the engine and now it works.

# name: test-plugin
# about: This is intended to be a feature-rich plugin for opencollective-discourse integration
# version: 1.0.0
# authors: Sudaraka Jayathilaka

    after_initialize do
      	require_dependency 'application_controller'
      
     	class ::TestController < ::ApplicationController
    	  	#This class will contain all the back end controller methods 
    	    requires_plugin 'test-plugin'
    	    require_dependency 'admin_constraint'
    	    #this will return the JSON of users in the plugin store 
    	    def list
        		render json: { name: "donut", description: "delicious!" }
    	    end
    	end

      	add_admin_route "test.title", "test"

      	Discourse::Application.routes.append do
       		get "/admin/plugins/test" => "admin/plugins#index"
       		get "/admin/plugins/test/list" => "test#list"
    	end
    end