Creating Routes in Discourse and Showing Data

Sounds like you’re looking for this guide:

4 Likes

Gah, of course, thanks - I was put off by the title thinking ‘I don’t need an Admin Interfaces for this project so i’ll look at this later’ … ooops

1 Like

Sorry for bumping an old topic, but using this discourse AJAX call how do we access data from the Rails server at a URL which needs to be dynamically defined?

eg
https://discourse.baseurl.org/u/:username/something.json

On the Ember side, something like:

ajax("/u/${username}", { type: 'GET' }).then((result) => { etc.

(replace the speechmarks with backticks and make sure you’ve included import { ajax } from "discourse/lib/ajax"; above)

On the Ruby side, something like:

get '/u/:username' => 'something#index'

I think this will be addressable as /u/username.json (which is not exactly what you asked for)

you might try:

get '/u/:username/something' => 'something#index'

if you insist on the something bit.

Can’t guarantee that’s 100% but may help you move forward …

This is, as always, a good reference:

3 Likes

Thanks @merefield

I’m OK on the Rails-side routing. I have to say it’s very hard to make progress on anything but trivial plugins, without better documentation of the Discourse-specific parts of the API, since not all of it is pure Ember or pure Rails. I’m all for reading the source code to work stuff out, but equally it would be nice if there were just some comments in the source to explain the API of some common components, how to invoke the Composer, etc.

5 Likes

Here is a similar guide by @kleinfreund which shows in great depth, how build discourse plugins. Great resource.

5 Likes

I’d love to see this re-written as a sample to add routes in a plugin. I’m sure that it’s “easy,” but it’s taking me a while to make the subtle transformations.

The Beginner's Guide to Creating Discourse Plugins Part 5: Admin Interfaces is a start, but there are still some missing pieces to actually adding a route with a new model.

1 Like

I was very excited to see the guide you linked (now 2 years old), but it doesn’t work. I thought it was me messing up creating my version with my plugin name, but then I cloned his code and it gives me a “Oops!” for /notebook.

It gets “Ooops” rather than “route not found”, so it’s doing something close. . . but I’m back to being lost. I’m sure it’s one line of code, or something in the wrong place, but I’m stuck.

1 Like

It was working a few months ago for sure. I’ll take a look maybe in the weekend. Bookmarking it for now. :wink:

3 Likes

@pfaffman were you able to figure out what the problem was? I have the same issue with the guide

2 Likes

I was not. It’s like to get it figured out, but the pressing issue I was looking to solve I may solve without discourse.

The notebook tutorial had an error due to this issue:

Which I fixed and tested with the PR:

https://github.com/kleinfreund/notebook/pull/2/files

and it works again:

Hope this helps.

PS: Slowly, I’m making progress with learning to write Discourse plugins… very, very slowly :slight_smile: LOL

3 Likes

Thank you @neounix

Much appreciated. Tried that plugin tutorial months ago and could not get it to work and gave up.

Now, it works fine with that code change:

cat notebook.js.es6 
/**
 * Route for the path `/notebook` as defined in `../notebook-route-map.js.es6`.
 */
import DiscourseRoute from "discourse/routes/discourse";
export default DiscourseRoute.extend({
  renderTemplate() {
    // Renders the template `../templates/notebook.hbs`
    this.render("notebook");
  },
});

Thanks again!

This will help a lot of wannabes like me.

1 Like

Awesome! Thanks @neounix! I figured that it was a one line fix, but had no clue how to figure out what that one line was.

:tada:

Now I can get back to my Discourse Server Manager plugin that I can hopefully use to build a GitHub App to install, upgrade, and manage plugins in Discourse installations.

2 Likes

Thanks! Much appreciated.

Opened a github issue on that broken plugin way back on 1 May, and at that time I had no idea I would be the person to eventually sort this out, LOL

https://github.com/kleinfreund/notebook/issues/1

It’s really a nice Discourse plugin tutorial. Many noobs like me can benefit from it, indeed.

2 Likes

I’m not bad at the Rails side, but Ember and Javascript still make very little sense to me. (Hopefully these replies will be deleted after some days?)

In the meantime, until Mr. Kleinfreund has had a chance to review and accept the PR, the working tutorial code is available here:

https://github.com/unixneo/notebook-neo

In addition, I have added some code (v0.7.2) to help beginners (like me) learn about the Rails MVC lifecycle which logs the calls to a log file (when enabled/ disabled in the site settings).

2 Likes

Just as quick status update…

The PR has been merged and the original tutorial has been updated; so hopefully this will help those learning plugins, as this is a well done, step-by-step tutorial.

https://github.com/kleinfreund/notebook

4 Likes

I’m going through the tutorial now at How to create a Discourse plugin – kleinfreund.de. It’s extremely helpful to have a step by step guide that includes creating a new route and controller in a plugin. Would love to have more like it!

In the earlier discussion here, there is talk about the fact that the tutorial uses Discourse.Route, where it should, in updated form, use DiscourseRoute instead. I didn’t fully understand that at first (I thought the tutorial was updated, but it sounds like the tutorial is not updated, just the accompanying github repo is). So, just to lay it out:

The tutorial says that at ./assets/javascripts/discourse/routes/notebook.js.es6 :
you should write:

/**
 * Route for the path `/notebook` as defined in `../notebook-route-map.js.es6`.
 */
export default Discourse.Route.extend({  /****JQ331 NOTE: THIS IS NOT CORRECT. SHOULD BE: DiscourseRoute.extend... and include the import statement at the top******/
  renderTemplate() {
    // Renders the template `../templates/notebook.hbs`
    this.render('notebook');
  }
});

So the correct version is:

import DiscourseRoute from 'discourse/routes/discourse';

/**
 * Route for the path `/notebook` as defined in `../notebook-route-map.js.es6`.
 */
export default DiscourseRoute.extend({
  renderTemplate() {
    // Renders the template `../templates/notebook.hbs`
    this.render('notebook');
  }
});

This is properly reflected in the github repo going along with the tutorial: notebook/notebook.js.es6 at main · kleinfreund/notebook · GitHub.

In case this helps someone else.

1 Like

I’ve created a new route, which properly loads a template, following the guidance in this topic. But: the browser back button never goes back to that route. How can I correct this?

Here’s what I mean:

say my new route is ‘/notebook’, and when the user goes to that route (mysite.com/notebook), it properly loads the ‘notebook’ template. If the user is on that page, and then leaves the page, when the user hits the browser’s back button, the browser does NOT return to that new route page. Instead, the browser will go to wherever the user was prior to going to the new route.

In other words, the browser back button treats the new route as if it doesn’t exist (it shows up in history, but the browser doesn’t navigate there with the back button).

What do I need to do to fix that?

Thanks!

1 Like