我创建了完美的Grape-create动作
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
但是,当我想创建一个PUT动作来编辑某些值时,我在JSON中的响应中出错:error: "project[id] is missing"
我的PUT代码:
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
我做错了什么?
答案 0 :(得分:2)
行。我通过此代码修复它并且它可以正常工作
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
答案 1 :(得分:1)
我不确定你的结构没有看到你在PUT请求中发送的内容,但我认为Grape代码应该是这样的 - 但同样,我完全猜测没有看到你的JSON有效载荷。
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