Je suis novice dans Rails. J'ai essayé d'écrire une petite application API Rails utiliser gem grape
.Attributs manquants (API raisin)
J'ai suivi ce tutoriel http://www.sitepoint.com/build-great-apis-grape/
Mais quand je l'ai essayé de créer un nouveau record, j'ai eu une erreur: {"error":"type_id is missing"}
Voici mon code:
singers.rb
module V1
class Singers < Grape::API
resource :singers do
desc "List all singers"
get do
Singer.all
end
desc "Create a new singer"
params do
requires :name, type: String
requires :type_id, type: Integer
end
post do
Singer.create!({
name: params[:name],
type_id: params[:type_id]
})
end
end
end
end
Et quand je tape dans la console comme: curl http://localhost:3000/api/v1/singers.json -d "name=khanhpn;type_id=1"
J'ai eu une erreur: {"error":"type_id is missing"}
Je ne comprenais pas pourquoi il jette l'erreur. J'espère que tout le monde peut m'expliquer. Merci beaucoup.
C'est mon code que je poussais sur bitbucket: https://bitbucket.org/baran19901990/grape_api/src/b8a0d676f17de3fedc95cc7efff60fab5afb0fc1/app/api/v1/singers.rb?at=master&fileviewer=file-view-default
Solutions:
curl -X POST http://localhost:3000/api/v1/singers -d "name=khanhpn&type_id=1"
Merci pour votre réponse. Cela a bien fonctionné. –