【问题标题】:RAILS DELETE BY POLYMORPHISM通过多态删除轨道
【发布时间】:2022-08-15 09:35:53
【问题描述】:

我有一个表,它使用多态来为 \"AtaPublic\" 或 \"AtaObject\" 添加书签。

在我的销毁中,我不想直接删除favorites.id id 的“收藏夹”,我希望这样的东西来自路线:{{URL}}/favorites/ata_public/2 \"AtaPublic\" 或 \"AtaObject\" 的 id 来执行收藏夹的销毁,而不是 favorite_id 的日期。

问题是,我的销毁控制器应该是什么样子?我的参数设置?这样,它会按收藏夹的 ID 删除。

收藏夹控制器

def destroy
  if @favorite.destroy
    render json: {success: I18n.t(\"favorite.destroy.success_message\")}, status: 200
  else
    render json: ErrorMessages.new(error: I18n.t(\"favorite.destroy.error_message\"))
      .call, status: 422
  end
end

private

def parent_ata_public
  return AtaPublic.find_by_id(params[:ata_public_id]) if params[:ata_public_id]
end

def parent_ata_object
  return AtaObject.find_by_id(params[:ata_object_id]) if params[:ata_object_id]
end

def set_favorite
  begin
    @favorite = Favorite.where(user: @current_user).find(parent_ata_public)
  rescue
      render json: ErrorMessages.new(error: I18n.t(\"favorite.not_found.error_message\")).call, status: 404
  end
end

路由中的同一个ID怎么删除? 路线:{{URL}}/favorites/ata_public/2

图像数据库: https://i.stack.imgur.com/qX9uD.png

    标签: ruby-on-rails ruby


    【解决方案1】:

    如果您已经在 URL 中添加了 ID,则一切就绪。

    在控制器中,只需使用request.path 和一些正则表达式即可获得所需的 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多