Destroying nested resources in restful way
I'm looking for help destroying a nested resource in Merb. My current method seems near correct, but the controller raise an InternalServerError during the destruction of the nested object.
Here comes all 开发者_C百科the details concerning the request, don't hesitate to ask for more :)
Thanks,
Alex
I'm trying to destroy a nested resources using the following route in
router.resources :events, Orga::Events do |event|
event.resources :locations, Orga::Locations
end
Which gives in jQuery request (delete_ method is a implementation of $.ajax with "DELETE"):
$.delete_("/events/123/locations/456");
In the Location controller side, I've got:
def delete(id)
@location = Location.get(id)
raise NotFound unless @location
if @location.destroy
redirect url(:orga_locations)
else
raise InternalServerError
end
end
And the log:
merb : worker (port 4000) ~ Routed to: {"format"=>nil, "event_id"=>"123", "action"=>"destroy", "id"=>"456", "controller"=>"letsmotiv/locations"}
merb : worker (port 4000) ~ Params: {"format"=>nil, "event_id"=>"123", "action"=>"destroy", "id"=>"456", "controller"=>"letsmotiv/locations"}
~ (0.000025) SELECT `id`, `class_type`, `name`, `prefix`, `type`, `capacity`, `handicap`, `export_name` FROM `entities` WHERE (`class_type` IN ('Location') AND `id` = 456) ORDER BY `id` LIMIT 1
~ (0.000014) SELECT `id`, `streetname`, `phone`, `lat`, `lng`, `country_region_city_id`, `location_id`, `organisation_id` FROM `country_region_city_addresses` WHERE `location_id` = 456 ORDER BY `id` LIMIT 1
merb : worker (port 4000) ~ Merb::ControllerExceptions::InternalServerError - (Merb::ControllerExceptions::InternalServerError)
Not all browsers actually support sending a real DELETE request. A common work around is to use a POST with a special param of "_method=DELETE".
Assuming your browser actually is sending the DELETE request, a backtrace or some more information from the error would be helpful for further debugging.
It looks to me like you are raising the InternalServerError. I think a better way to phrase the question would be "why is @location.destroy returning false?".
Try it in the console and see, I'm guessing you are failing some kind of *before_destroy* callback, or perhaps running afoul of another rule in your Entity model.
精彩评论