# Plugin controller spec: Plugin routes are not drawn

**URL:** https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606
**Category:** Development
**Created:** [February 7, 2019, 3:35pm UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606 "2019-02-07T15:35:54Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [February 7, 2019, 3:35pm UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606/1 "2019-02-07T15:35:54Z")

</div>

Running the tests for my plugin like this:

```sh
LOAD_PLUGINS=1 bundle exec rake plugin:spec["my-plugin"]

```

… the plugin’s routes don’t seem to be available in the following spec file:

`spec/controllers/plugin/routes_controller_spec.rb`:

```ruby
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:

```plaintext
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`:

```ruby
# …

Discourse::Application.routes.append do
  get '/plugin_routes' => 'plugin/routes#index', constraints: AdminConstraint.new
end

```

`app/controllers/plugin/routes_controller.rb`:

```ruby
class Plugin::RoutesController < ApplicationController
  def index
    # …
  end
end

```

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [February 7, 2019, 4:48pm UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606/2 "2019-02-07T16:48:00Z")

</div>

I’m not sure what you’re doing wrong from your example code, but we have plugins that draw routes and then test them:

> <https://github.com/discourse/discourse-akismet/blob/main/plugin.rb#L77-L79>

and

[https://github.com/discourse/discourse-akismet/blob/master/spec/requests/admin\_mod\_queue\_controller\_spec.rb#L14](https://github.com/discourse/discourse-akismet/blob/master/spec/requests/admin_mod_queue_controller_spec.rb#L14)

I would try to pare down your code until it works.

---

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [February 8, 2019, 9:36am UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606/3 "2019-02-08T09:36:45Z")

</div>

I have my plugin’s routes set up like this:

```ruby
Discourse::Application.routes.append do
  get '/plugin_routes' => 'plugin/routes#index', constraints: AdminConstraint.new
  put '/plugin_routes/:route_id' => 'plugin/routes#update', constraints: AdminConstraint.new
  delete '/plugin_routes/:route_id' => 'plugin/routes#destroy', constraints: AdminConstraint.new
end

```

I’m not using an engine. Does that matter?

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [February 8, 2019, 1:54pm UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606/4 "2019-02-08T13:54:42Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [February 8, 2019, 2:30pm UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606/5 "2019-02-08T14:30:04Z")

</div>

I tried using routes, but couldn’t figure out why the routes aren’t drawn with this setup:

```ruby
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.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [February 8, 2019, 2:33pm UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606/6 "2019-02-08T14:33:17Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kleinfreund](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kleinfreund](https://meta.discourse.org/u/kleinfreund)
#### Post date: [February 8, 2019, 2:51pm UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606/7 "2019-02-08T14:51:47Z")

</div>

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`.

```ruby
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.

---

<div class="post-metadata">

### Author: ![eviltrout](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/eviltrout/32/5275_2.png) [@eviltrout](https://meta.discourse.org/u/eviltrout)
#### Post date: [February 8, 2019, 3:46pm UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606/8 "2019-02-08T15:46:13Z")

</div>



---

<div class="post-metadata">

### Author: ![zogstrip](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/zogstrip/32/512781_2.png) [@zogstrip](https://meta.discourse.org/u/zogstrip)
#### Post date: [February 11, 2019, 11:29am UTC](https://meta.discourse.org/t/plugin-controller-spec-plugin-routes-are-not-drawn/108606/9 "2019-02-11T11:29:58Z")

</div>

> [@kleinfreund](#):
>
> Running the tests for my plugin like this:
> 
> ```plaintext
> LOAD_PLUGINS=1 bundle exec rake plugin:spec["my-plugin"]
> 
> ```

There’s no need to use the `LOAD_PLUGINS=1` in this case since the `plugin:spec` rake task already does it 😉

> <https://github.com/discourse/discourse/blob/2c12336c6b7c8223d4fa00b2e928e276224f05a5/lib/tasks/plugin.rake#L92>
