Ember and Rails-Grape API - PUT/POST problem


#1

Hi everyone :smile: Unfortunatelly I don’t know why my put and post requests not good for my server-side. I generate templates in ember through: ember g scaffold… I can delete/show items, but I can’t edit, create new projects. What’s wrong with this?

 module API
  module V1
    class Projects < Grape::API
      include API::V1::Defaults

      resource :projects do
        desc "Return all projects"
    get "", root: :projects do
      Project.all
    end

    desc "Return a project"
    params do
      requires :id, type: String, desc: "ID of the project"
    end
    get ":id", root: "project" do
      Project.where(id: permitted_params[:id]).first!
    end

    desc "Create a project."
    params do
      requires :name, type: String, desc: "Name of project."
      requires :user_id, type: String, desc: "user id"
      requires :description, type: String, desc: "Description of project"
      requires :project_type_id, type: String, desc: "Type of project"
    end
    post do
      Project.create!({
                          name: params[:name],
                          user_id: params[:user_id],
                          description: params[:description],
                          project_type_id: params[:project_type_id]
                      })
    end

    desc "Update a project."
    params do
      requires :id, type: String, desc: "project ID."
      requires :name, type: String, desc: "Name of project."
      requires :user_id, type: String, desc: "user id"
      requires :description, type: String, desc: "Description of project"
      requires :project_type_id, type: String, desc: "Type of project"
    end
    put ':id' do
      Project.find(params[:id]).update({
                                           name: params[:name],
                                           user_id: params[:user_id],
                                           description: params[:description],
                                           project_type_id: params[:project_type_id]
                                       })
    end

    desc "Delete a project."
    params do
      requires :id, type: String, desc: "project ID."
    end
    delete ':id' do
      Project.find(params[:id]).destroy
    end

      end


    end
  end
end

#2

@updater did you solve this issue? A have the same problem, and can’t figure out how to get grape to ignore the root object.


#3

You have to nest the params in model object param then it works


#4

example

desc 'Add new Opportunity with given params'
    params do
      requires :opportunity, type: Hash do
        requires :name, type: String, desc: 'Opportunity name'
      end
    end

    post do
      opportunity_params = params[:opportunity]

      Opportunity.create opportunity_params
    end

#5

Alternatively you can override your adapter:

import DS from 'ember-data';

export default DS.ActiveModelAdapter.extend({
  // this method is almost copy-pasted from ActiveModelAdapter
  updateRecord: function(store, type, snapshot) {
    var data = {};
    var serializer = store.serializerFor(type.typeKey);

    serializer.serializeIntoHash(data, type, snapshot);

    var id = snapshot.id;
    var url = this.buildURL(type.typeKey, '', snapshot, 'updateRecord');

    return this.ajax(url + '/' + id, 'PUT', {
      // this line is changed!
      data: data[type.typeKey]
    });
  }
});