Plugin Development: record.save() returns 404


#1

I’m trying to develop a plugin and save records to the store (as described in Upgrading our front end models to use a store). I can create the record successfully, call save(), but it will only return a 404 at /routes.

./plugins/plugin-name/assets/javascripts/discourse/controllers/admin-plugins-plugin-name.js.es6:

const routeRecord = this.store.createRecord('route', { propA, propB });
routeRecord.save()
  .then(result => {
    console.log(result);
  }
  .catch(error => {
    console.error(error);
  }
  .finally(() => {
    console.log('Done');
  }

I seem to be missing some sort of model for the type of record I want to store. Without the model below, the POST request to routes isn’t sent out. Implementing the model on my own, the request is sent, but returns a 404. I don’t know what is necessary for it to work properly. It looks like this:

./plugins/plugin-name/assets/javascripts/discourse/models/route.js.es6:

import RestModel from 'discourse/models/rest';

export default RestModel.extend({
  _init: function() {
    this._super();
  }.on('init'),

  createProperties() {
    return {
      propA: this.getProperties('propA'),
      propB: this.getProperties('propB')
    };
  }
});

What is required for /routes to be a valid end point?


(Robin Ward) #3

By default, when you call save on your model, it’ll make a POST to /<recordname>.

I tried your example code and it seems that route is a reserved name, so I would recommend you use a different name.

Did you implement a server side route for your record to save to? For example a type of widget would save to /widgets as a POST. You can run rake routes on the command line to see the routes you’ve created.


#4

In the meantime, I learned a couple of things. I neither have a background in any kind of back end programming nor do I know Ruby/Rails/Ember. That’s why when writing my observations, there wasn’t any server-side controller answering the requests. I managed to implement working data transfer between the client and the server as I need it.

I am able to use routes as an endpoint (i.e. I’m doing this.store.createRecord('route', ...) and requests are being sent to /routes, /routes/:route_id, etc.). Though from what you say, it sounds logical to use a different name for the endpoint, right?

Maybe I should write a tutorial on implementing basic data exchange using the store (is there a better name for that?) for transferring data between client and server and PluginStore for transferring data between application and persistent storage. That might be helpful for a few people.


(Robin Ward) #5

Your code is likely working because you defined a new route class, but that is a conflict with Ember’s route and setting yourself up for weird errors in the future. I highly recommend you use a different name for your model/endpoint.

Sure, more documentation is always great!