[Proposal] Interactive "Generate Controller" button on MissingController error pages

I’m not sure if this suggested feature was posted before, however, but here is the idea: ** Context:** Currently, when a developer hits a ActionDispatch::MissingController (uninitialized constant) error, the workflow requires switching contexts:

  1. Read the error in the browser.

  2. Switch to the terminal.

  3. Type bin/rails generate controller ...

  4. Switch back to the browser and refresh.

The Proposal: I propose adding a “Generate Missing Controller” button directly to rescue template. When clicked, this button would trigger the Rails controller generator via a development-only internal route or will create a new action inside an existing controller.

How it works:

  • Detection: The DebugExceptions middleware identifies the MissingController exception.

  • Interaction: A button appears in the UI that sends a POST request to /__rails/create_controller.

  • Action: The middleware invokes Rails::Generators.invoke("controller", [name]).

  • Experience: The page auto-reloads, and the developer can immediately start working on their code without leaving the browser.

Benefits:

  • Reduced Friction: Especially helpful for beginners who are still learning the naming conventions of Rails.

  • Speed: Faster “Red-to-Green” cycle during the initial scaffolding phase of a project.

  • Precedent: This follows the spirit of ActiveRecord::PendingMigrationError, which already provides a “Run Pending Migrations” button in the browser.

Security: This would be strictly limited to the development environment and protected within the DebugExceptions middleware, ensuring no impact on production security or performance.

Previous similar behavior: Rails already does something similar for database migrations.

Well, I don’t see a significant benefit from creating an empty controller, you don’t leave your browser but you still have to go to your editor to add some useful code to this controller.

Running pending migrations is different because it just runs some complete code and also the root cause of the error is well identified.

In the case of MissingController error, the root cause could be a typo in a link.

1 Like

I actually quite like this. ‘generate controller’ can do a lot more than just creating an empty controller file, like adding tests and views.

Perhaps it should refer to the routes and only suggest generating a controller if the route already exists? This way it could also know whether to offer index, edit, show, etc. I’m assuming that this is for fixing a case when you’ve added a route and forgotten to write a controller.