require 'rails_helper'
describe Plugin::RoutesController do
it 'should route to #index' do
expect(get '/plugin_routes').to route_to(action: :index, controler: 'plugin/routes')
end
end
Error:
ActionController::UrlGenerationError:
No route matches {:action=>"/plugin_routes", :controller=>"plugin/routes"}
What am I doing wrong? Below are excerpts of my plugin’s code:
plugin.rb:
# …
Discourse::Application.routes.append do
get '/plugin_routes' => 'plugin/routes#index', constraints: AdminConstraint.new
end
app/controllers/plugin/routes_controller.rb:
class Plugin::RoutesController < ApplicationController
def index
# …
end
end
If you are creating a sub-path for your plugin, I recommend an engine. All our major plugins do so. If you are wanting to change existing routes, an engine is not required.
I don’t see anything immediately wrong with your example. My suggestion would be to start with one of the official plugins and update it to match what you are doing until it works, and go from there.
I tried using routes, but couldn’t figure out why the routes aren’t drawn with this setup:
Plugin::Engine.routes.draw do
get '/plugin_routes' => 'plugin/routes#index'
put '/plugin_routes/:route_id' => 'plugin/routes#update'
delete '/plugin_routes/:route_id' => 'plugin/routes#destroy'
end
Discourse::Application.routes.append do
mount Plugin::Engine, at: '/plugin', constraints: AdminConstraint.new
end
Running rake routes lists the engine, but no entries under Routes for Plugin::Engine:. The routes are listed for the poll plugin, for example. I can’t tell what the difference between my plugin and other plugins is which use engines.
Again, please download one of our existing plugins and start removing code / modifying routes until it matches what you want. That is the best advice I can give.
For who might experience the same problem as described above, one way I am able to get the routes drawn with an engine was defining the engine inside plugin.rb rather than lib/engine.rb.
after_initialize do
# …
module ::Plugin
class Engine < ::Rails::Engine
engine_name Plugin::PLUGIN_NAME
isolate_namespace Plugin
end
end
Plugin::Engine.routes.draw do
get '/my_routes' => 'routes#index'
put '/my_routes/:route_id' => 'routes#update'
delete '/my_routes/:route_id' => 'routes#destroy'
end
Discourse::Application.routes.append do
mount Plugin::Engine, at: '/plugin', constraints: AdminConstraint.new
end
end
Why that makes any difference is beyond me.
As for the strategy that was suggested: Yes, I could certainly try re-implementing my plugin starting with one of the default plugins and observe at which point the described failure occurs. However, that’s very impractical as I’ve working on the plugin for close to a year.