1

I create Grape-create action which work perfectly

desc "Create a project."
        params do
          group :project, type: Hash 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
        end
        post do
          Project.create!(
              name: params[:project][:name],
              user_id: params[:project][:user_id],
              description: params[:project][:description],
              project_type_id: params[:project][:project_type_id]
          )
        end

But, when I want to create a PUT action to edit some values, I have error from response in JSON: error: "project[id] is missing" My PUT code:

desc "Update a project."
        params do
          group :project, type: Hash 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
        end
        put ':id' do
          Project.find(params[:project][:id]).update!(
              name: params[:project][:name],
              user_id: params[:project][:user_id],
              description: params[:project][:description],
              project_type_id: params[:project][:project_type_id]
          )
        end

What I do wrong with this?

adammokan
  • 844
  • 1
  • 6
  • 12
bimbim
  • 33
  • 5
  • Have you tried to hit this route using curl or something outside of ember? I'm just asking because this doesn't appear to be ember related at all. – adammokan Jun 03 '15 at 22:41
  • Yes. I edit items by swagger doc and work great. I just create simple grape api in rails and simple ember app. In ember app I generate `ember g scaffold projects....` and that's all. I read topic from [link](http://stackoverflow.com/questions/26852647/ember-js-post-requests-return-400-from-the-server-grape-api-but-are-stored-suc) and I edit my Grape API by adding `group :contact, type: Hash do`. I don't know why edit didn't work since the creation of new one work :/ – bimbim Jun 04 '15 at 12:46
  • That doesn't help anyone here, really. You'll still want to provide an example JSON payload. – adammokan Jun 04 '15 at 12:49
  • Ok. My Frontend send this: `{"project":{"name":"tt","user_id":44,"description":"asdasdasd","project_type_id":3,"created_at":"2015-06-03T14:15:06.689Z","updated_at":"2015-06-03T14:15:06.689Z"}}` My backend want this: `{"name":"tt","user_id":45,"description":"asdasdasd","project_type_id":2,"created_at":"2015-06-03T14:15:06.689Z","updated_at":"2015-06-03T14:15:06.689Z" }` – bimbim Jun 04 '15 at 13:10
  • In the future, it will help to provide that sort of example with your question. I apologize if I came across as rude, it just helps to demonstrate the full context to get the best possible answers. – adammokan Jun 04 '15 at 15:43

2 Answers2

2

OK. I repair this by this code and it work

desc "Update a project."
        params do
          group :project, type: Hash 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
        end
        put ':id' do
          Project.find(params[:id]).update!(
              name: params[:project][:name],
              user_id: params[:project][:user_id],
              description: params[:project][:description],
              project_type_id: params[:project][:project_type_id]
          )
        end
bimbim
  • 33
  • 5
1

I'm not sure of your structure without seeing what you are sending in your PUT request, but I would think the Grape code should be like - but again, I'm totally guessing without seeing your JSON payload.

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
adammokan
  • 844
  • 1
  • 6
  • 12