【问题标题】:Destroying nested resources in restful way以宁静的方式销毁嵌套资源
【发布时间】:2023-04-02 19:17:01
【问题描述】:

我正在寻求有关销毁 Merb 中嵌套资源的帮助。我当前的方法似乎接近正确,但是控制器在嵌套对象的销毁过程中引发了 InternalServerError

这里有关于请求的所有细节,不要犹豫,询问更多:)

谢谢,

亚历克斯


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

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

在 jQuery 请求中给出(delete_ 方法是带有“DELETE”的 $.ajax 的实现):

$.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)

【问题讨论】:

    标签: jquery rest resources router merb


    【解决方案1】:

    并非所有浏览器都支持发送真正的 DELETE 请求。一种常见的解决方法是使用带有特殊参数“_method=DELETE”的 POST。

    假设您的浏览器实际上正在发送 DELETE 请求,回溯或来自错误的更多信息将有助于进一步调试。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2021-06-01
        • 2016-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-17
        • 2013-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多