Set status code in Grape gem
Asked Answered
E

2

6

How does one customise the status code response when using the Grape gem? e.g.

post do
   status = :accepted
   @order = Order.find(params[:id])
end

This can be achieved for with error!({ error: 'Order not found'}, 404) but how does one do it for non errors?

Emboly answered 26/7, 2014 at 6:27 Comment(1)
rdoc.info/github/intridea/grape/Grape/…Zantos
E
14

Found the solution:

post do
  status 203
  @order = Order.find(params[:id])
end
Emboly answered 26/7, 2014 at 6:51 Comment(0)
A
0

From your code I see that you are using @order but you did not define it previously so it did not found in view and giving error

Hope your code look like as you update a status so you can use put instead of post

 put ':id' do
   @order = Order.find(params[:id])
   @order.update_attributes!(:status, :accepted)
 end

or for post

 post do
   @order = Order.new
   @order.status = :accepted
   @order.save!
 end
Acrimony answered 26/7, 2014 at 6:39 Comment(1)
Thanks I'll update my question to reflect your change.Emboly

© 2022 - 2024 — McMap. All rights reserved.