以restful方式销毁嵌套资源

4

我需要帮助在Merb中销毁嵌套资源。我的当前方法似乎接近正确,但是在销毁嵌套对象时控制器引发了一个InternalServerError异常。

这里是有关请求的所有细节,请随时询问更多信息:)

谢谢,

Alex


我正在尝试使用以下路由来销毁嵌套资源

router.resources :events, Orga::Events do |event|
  event.resources :locations, Orga::Locations
end

在jQuery请求中使用delete_方法(delete_方法是$.ajax的实现,使用"DELETE"参数):

$.delete_("/events/123/locations/456");

在位置控制器方面,我有以下内容:
def delete(id)
  @location = Location.get(id)
  raise NotFound unless @location
  if @location.destroy
    redirect url(:orga_locations)
  else
    raise InternalServerError
  end
end

并且日志如下:

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)
2个回答

0
并非所有浏览器都支持发送实际的 DELETE 请求。一个常见的解决方法是使用带有特殊参数 "_method=DELETE" 的 POST。
假设您的浏览器确实正在发送 DELETE 请求,那么回溯或一些更多的错误信息将有助于进一步调试。

0

在我看来,正在引发InternalServerError。我认为更好的表达方式是“为什么@location.destroy返回false?”。

在控制台中尝试一下,我猜测你可能会失败某种*before_destroy*回调,或者可能会违反实体模型中的另一个规则。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接