# Rails plugin generator

**URL:** https://meta.discourse.org/t/rails-plugin-generator/95907
**Category:** Announcements
**Created:** [August 29, 2018, 11:43am UTC](https://meta.discourse.org/t/rails-plugin-generator/95907 "2018-08-29T11:43:01Z")
**Posts on this page:** 11
**Page:** 3

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [April 29, 2020, 2:45pm UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/44 "2020-04-29T14:45:17Z")

</div>

Seems like a good start to me. Now, I just need to learn to actually do cool things with it.

My first plan is to write some code to query the DB so I can get a value I need from the `post_custom_fields` table and use that on the topics page for a small piece requested by our grey-beard mod team.

Tomorrow I’ll pull it down from git and start searching for sample DB code from other plugins to get me started and see how far I can get.

Thanks again @j.jaffeux

Every little bit helps and having a generator like this for novice discourse plugin makers like me is much appreciated. You can see it down there on the bottom, begging to be developed:

 ![Screen Shot 2020-04-29 at 9.44.28 PM](https://global.discourse-cdn.com/meta/original/3X/4/4/44a7d6018db43cf35795782fdd0a70042198d080.jpeg)

Over the years, I have written countless plugins for vBulletin, so I’m really excited about making some simple plugins for discourse, believe me.

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [August 11, 2020, 6:35am UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/45 "2020-08-11T06:35:41Z")

</div>

Hi @j.jaffeux

Still trying to learn about Discourse plugins, I went back to this plugin generator today; and tried this in dev environment on macOS, where the app is running fine and I have been doing a lot of Ruby and Rails studies and small plugins, to learn:

```plaintext
# cd /var/Tim/Discourse/plugins
# rails g plugin DiscourseRacoon
# bundle exec 'rails s'

```

All seems to go well, and the plugin installs fine:

![Screen Shot 2020-08-11 at 1.26.36 PM](https://global.discourse-cdn.com/meta/original/3X/8/4/84a22f49edc08cec6ea7130681c3f3d4fcc571a6.jpeg)

But when I go to the route:

```plaintext
http://localhost:3000/discourse_racoon

```

It’s not working:

 ![Screen Shot 2020-08-11 at 1.27.27 PM](https://global.discourse-cdn.com/meta/original/3X/d/2/d2a2fb213079bc444873516e8735f727e9a59dc0.jpeg)

Am I wrong that the plugin routes and the controller should work OOTB after the generate the basic plugin, in this case DiscourseRacoon?

I went to various online references, and they indicated we should check that the route existed; so I checked `routes.rb` and there are routes defined there; and I also checked `discourse_racoon_controller.rb` and the controller has an index method (action).

Sorry, I’m still struggling with more interesting plugin dev and Discourse. What I doing wrong? Any suggestion on what is my next step to debug this?

Thanks.

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [August 11, 2020, 6:39am UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/46 "2020-08-11T06:39:35Z")

</div>

It’s /discourse-racoon not /discourse\_racoon

```plaintext
rake routes | grep racoon

                         discourse_racoon /discourse-racoon DiscourseRacoon::Engine
        GET / discourse_racoon/discourse_racoon#index
actions GET /actions(.:format) discourse_racoon/actions#index
        GET /actions/:id(.:format) discourse_racoon/actions#show

```

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [August 11, 2020, 6:45am UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/47 "2020-08-11T06:45:06Z")

</div>

Hi @j.jaffeux

Thanks!

That was it.

I assume that is a Rails convention I need to learn? Where all routes defined with underscores are accessed with dashes?

```plaintext
DiscourseRacoon::Engine.routes.draw do
  get "/" => "discourse_racoon#index", constraints: DiscourseRacoonConstraint.new
  get "/actions" => "actions#index", constraints: DiscourseRacoonConstraint.new
  get "/actions/:id" => "actions#show", constraints: DiscourseRacoonConstraint.new
end

```

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [August 12, 2020, 4:27am UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/48 "2020-08-12T04:27:12Z")

</div>

Hey,

I generated a hello world plugin with this generator, added a bunch of Ruby “write to a file” statements and watched the Rails lifecycle, found and the Rails controllers were not working as expected (they would not log, indicating they were were being ignored).

After debugging, I got this to work as expected by moving the Rails controllers up a directory.

In addition, also added a Javascript/Ember controller and template for the main index page, added some HTML and CSS to all the templates and added some JS code to read the cookies and highlight the templates.

For example, after a few changes to the plugin generated by the rails plugin generator:

> `http://localhost:3000/hello-world/`

 ![Screen Shot 2020-08-12 at 11.05.01 AM](https://global.discourse-cdn.com/meta/original/3X/a/0/a0848d8b4e872354afd1cbc41f3b8f651607d816.jpeg)

For full details and screenshots, please see:

> **[A plugin for debugging the Discourse rails plugin generator](https://community.unix.com/t/a-plugin-for-debugging-the-discourse-rails-plugin-generator/379570)**
>
> Discourse has a rails plugin generator which generates a basic Discourse plugin scaffolding. So, I created a "hello world" plugin with this generator using rails g plugin hello-world and added a bunch of Ruby statements to each .rb file to...

In closing, I would like to thank @j.jaffeux for this rails plugin generator. I had a lot of fun debugging the Rails controllers and enjoyed modifying it to provide more code to inspect both the Rails lifecycle and the JS templates/controllers.

I hope my changes help anyone else who, like me, is still learning the basics of Discourse in plugin dev in their spare time and would like to use the rails plugin generator.

See also:

[https://github.com/unixneo/hello-world-rails-plugin-gen](https://github.com/unixneo/hello-world-rails-plugin-gen)

Currently, I am really having fun debugging and fixing broken plugins as part of the Rails and Discourse plugin dev learning process 🙂

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [August 12, 2020, 7:45pm UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/49 "2020-08-12T19:45:33Z")

</div>

Thanks for figuring this out, @neounix! Maybe this will be the leg up that I need to get my project moving forward.

> [@neounix](#):
>
> I got this to work as expected by moving the Rails controllers up a directory.

Hey @j.jaffeux, is moving those files the Recommended Solution, or should they be included with something like

```
  load File.expand_path('some-path-here', __dir__ ) 

```

I _think_ that I tried including them , but then got some error about a missing. . . something (so I presume I did the wrong thing and it’s not worth documenting exactly).

EDIT: It looks to me like the controller does indeed get loaded/run when hitting `/plugin-path`.

---

<div class="post-metadata">

### Author: ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)
#### Post date: [August 13, 2020, 12:25am UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/50 "2020-08-13T00:25:05Z")

</div>

Hey @pfaffman

I originally tried to load the Ruby controllers with load statements in `plugin.rb` but they would not log as expected.

When I tested the Ruby controllers I tested with Ruby statements to log to the filesystem and ran a `tail -f` on that log file to test how the controllers fire.

For fun, I ended my day yesterday writing some code to read all the process ENV vars into a cookie with a Rails controller and writing them to the app with the Ember controller.

Really fun!

I am addicted LOL

---

<div class="post-metadata">

### Author: ![riking](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/riking/32/170938_2.png) [@riking](https://meta.discourse.org/u/riking)
#### Post date: [September 10, 2020, 12:58am UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/51 "2020-09-10T00:58:06Z")

</div>

> [@neounix](#):
>
> I assume that is a Rails convention I need to learn? Where all routes defined with underscores are accessed with dashes?
> 
> ```plaintext
> DiscourseRacoon::Engine.routes.draw do
> get "/" => "discourse_racoon#index", constraints: DiscourseRacoonConstraint.new
> 
> ```

The URL portion is on the left side, the Ruby code reference is on the right side of the `=>`. The `_` vs `-` is defined at the engine `mount` line.

---

<div class="post-metadata">

### Author: ![Grayden\_Shand](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/grayden_shand/32/166586_2.png) [@Grayden\_Shand](https://meta.discourse.org/u/Grayden_Shand)
#### Post date: [August 19, 2021, 6:27pm UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/52 "2021-08-19T18:27:45Z")

</div>

In [Part 4 of @eviltrout’s plugin guide](https://meta.discourse.org/t/beginners-guide-to-creating-discourse-plugins-part-4-git-setup/31272), he recommends developing the plugin outside of the discourse code base and setting up a symbolic link in the `plugins` directory.

> [@Developing Discourse Plugins - Part 4 - Setup git](https://meta.discourse.org/t/developing-discourse-plugins-part-4-setup-git/31272/1):
>
> The beauty of this setup is you can just check your plugin into github and not worry about the discourse codebase it lives inside. Your changes will be isolated to the plugin itself. If you need to edit discourse’s code you still can, but git will track the changes separately!
> 
> I recommend using one editor window for your plugin codebase and one for Discourse itself. It is easier when you think of them as two different things.

Would it make sense to add a `-d ~/path/to/plugin_src` option to generate the plugin in a different directory, and possibly also set up the symlink?

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [August 20, 2021, 8:38am UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/53 "2021-08-20T08:38:57Z")

</div>

I’m sorry but I might deprecate this and close the topic for now.

While less powerful in terms of customisations, our preferred way to kickstart a plugin now is to use [GitHub - discourse/discourse-plugin-skeleton: Template for Discourse plugins · GitHub](https://github.com/discourse/discourse-plugin-skeleton) as a template (see the green button USE THIS TEMPLATE).

It’s much easier for us to keep this up to date.

---

<div class="post-metadata">

### Author: ![j.jaffeux](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/j.jaffeux/32/60297_2.png) [@j.jaffeux](https://meta.discourse.org/u/j.jaffeux)
#### Post date: [August 20, 2021, 8:39am UTC](https://meta.discourse.org/t/rails-plugin-generator/95907/54 "2021-08-20T08:39:05Z")

</div>



[Previous page](https://meta.discourse.org/t/rails-plugin-generator/95907.md?page=2)
