Controllers and Routes

When following the tutorial for setting up a custom controller and route, I get the Routing Error page. The page lists the paths and controllers, which is all correct however, when I request the resource, I get errors. I understand this is an old tutorial but, is the information accurate? I’ve attached my plugin skeleton and maybe someone can help trouble shoot the issue. Thanks!

jb-test.zip (522.2 KB)

I think the thing you’re missing is to add it to rails routing.

Something like this in plugin.rb.

  JbTest::Engine.routes.draw do
    get "/hello" => "jbtest#index"
  end

Here’s a start. The other thing to do is to get GitHub - discourse/all-the-plugins and look through there. Now that so many have been added to core, though, I suppose you could just look there for examples of added to the Rails route.

Minimal Custom Route Plugin for Discourse AI

pfaffman:

what’s needed to add a route to Discourse rails?

Can you give me a minimal proof of concept that includes a plugin.rb, and controller, and the stuff needed to add the route to rails?

DiscourseHelper:

Searching

Searching for: How to add a custom route to Discourse using a plugin, including a minimal example with plugin.rb, custom controller, and the necessary code to register the Rails route?

Reading

Read: Developing Discourse Plugins - Part 1 - Create a basic plugin

Searching

Searching for: Minimal proof of concept for adding a custom Rails route in Discourse plugin including plugin.rb, controller implementation, and code to register the route.

Reading

Read: Creating Routes in Discourse and Showing Data

Reading

Read: Add custom user routes using plugins

Reading

Read: Adding a ruby controller via plugin

Here’s a minimal proof of concept for adding a custom Rails route to Discourse via a plugin, including the key files you’ll need: plugin.rb, a controller, and the code for registering the route.

Key Points

  • The best practice is to use a Rails “engine” within your plugin to isolate your controllers and routes.
  • You need to explicitly require your controller in plugin.rb.
  • To register a new route, you use the engine’s routes.draw method and mount the engine in the main application routes.

Minimal Example

plugin.rb

# name: my-custom-route
# about: Adds a custom route via Rails engine in a plugin
# version: 0.1
# authors: Your Name

after_initialize do
  module ::MyPlugin
    class Engine < ::Rails::Engine
      engine_name "my_plugin"
      isolate_namespace MyPlugin
    end
  end

  require_dependency File.expand_path('../app/controllers/my_plugin/my_controller.rb', __FILE__)

  MyPlugin::Engine.routes.draw do
    get "/hello" => "my#hello"
  end

  Discourse::Application.routes.append do
    mount ::MyPlugin::Engine, at: "/my_plugin"
  end
end

app/controllers/my_plugin/my_controller.rb

module MyPlugin
  class MyController < ::ApplicationController
    def hello
      render plain: "Hello from my plugin!"
    end
  end
end

How this works:

  • GET /my_plugin/hello will return the plain text response "Hello from my plugin!".

Required Directory Structure:

plugins/
  my-custom-route/
    plugin.rb
    app/
      controllers/
        my_plugin/
          my_controller.rb

For more details and expanded examples, see:

This proof of concept will help you add a simple custom endpoint to your Discourse instance via a plugin. If you have questions about integrating this with Ember/JS side, let me know!

1 Like

OK. Thank you. It looks like that bit of code was added to routes.rb but, I’ll look through the core plugins and see if I can find an example in plugins.rb. Thanks!

1 Like