You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Override create_path to return the relative path to the new resource. Note that this will be called before the resource is actually created, which means that you need to know the ID before the object has been inserted into the database. This might seem a hassle, but it stops you from exposing your database column IDs to the world, which is a naughty and lazy habit we've all picked up from Rails.
The response Content-Type and status will be set for you.
Override allowed_methods and process_post. Put all the code to be executed in process_post.
process_post must return true, or the HTTP response code
Response headers like Content-Type will need to be set manually.
classDispatchOrderResource < Webmachine::Resourcedefallowed_methods["POST"]enddefresource_exists?@order=Order.find(id)enddefprocess_post@order.dispatchresponse.headers['Content-Type']='text/plain'response.body="Successfully dispatched order #{id}"trueendprivatedefidrequest.path_info[:id]endend
PUT
Override resource_exists?, content_types_accepted, allowed_methods, and implement the method to create/replace the resource.
classOrderResource < Webmachine::Resourcedefallowed_methods["PUT"]enddefcontent_types_accepted[["application/json",:from_json]]enddefresource_exists?@order=Order.find(id)enddeffrom_json# Remember PUT should replace the entire resource, not merge the attributes! That's what PATCH is for.# It's also why you should not expose your database IDs as your API IDs.@order.destroyif@ordernew_order=Order.new(params)new_order.save(id)response.body=new_order.to_jsonendprivatedefparamsJSON.parse(request.body.to_s)enddefidrequest.path_info[:id]endend