【问题标题】:Rails 4: how to check if an instance has been deletedRails 4:如何检查实例是否已被删除
【发布时间】:2015-09-28 03:53:47
【问题描述】:

在我们的 Rails 应用程序中,我们有一个 CalendarsController

class CalendarsController < ApplicationController

  def create
    @calendar = current_user.calendars.create(calendar_params)
    current_user.add_calendar_and_role(@calendar.id, 'Owner')
    if @calendar.save
      current_user.total_calendar_count += 1
      current_user.owned_calendar_count += 1
      current_user.save
      flash[:success] = "Calendar created!"
      redirect_to dashboard_path
    else
      render 'static_pages/home'
    end
  end

  def show
    @calendar = Calendar.find(params[:id])
    @posts = @calendar.posts
    @post = Post.new
  end

  def index
  end

  def edit
  end

  def destroy
    Calendar.find(params[:id]).destroy
    flash[:success] = "Calendar deleted"
    redirect_to dashboard_path
  end

  private

    def calendar_params
      params.require(:calendar).permit(:name)
    end

end

create动作中,当一个新的@calendar被创建时,我们运行@calendar.save来检查新实例是否真的被创建,然后执行一些动作。

我们希望在 destroy 操作中实施类似的流程。

我们正在考虑更新destroy方法如下:

def destroy
  @calendar = Calendar.find(params[:id])
  @calendar.destroy
  if @calendar.delete
    flash[:success] = "Calendar deleted"
    current_user.total_calendar_count -= 1
    if @calendar.administrations.role == "Owner"
      current_user.owned_calendar_count -= 1
    end
  end
  redirect_to dashboard_path
end

这段代码的语法是否正确,尤其是if @calendar.deleteif @calendar.administrations.role == "Owner"

而且,最重要的是,这个destroy 操作的代码是否有意义?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 model-view-controller destroy


    【解决方案1】:

    您是否考虑过使用persisted? 方法

    @calendar.destroy
    unless @calendar.persisted?
       ... some code here ....
    end
    

    【讨论】:

    • 不,我没有。其他代码结构的优势是什么?这是 Rails 的方法吗?
    • 坚持吗?检查您的记录是否仍在数据库中或仅在内存中。如果它没有“持久化”在数据库中,它将返回“false”。是你想做的。这是一个 Rails ActiveRecord 方法:apidock.com/rails/ActiveRecord/Persistence/persisted%3F
    • 听起来确实是我们想要实现的目标,非常感谢。
    • 没问题!请检查我的答案是否正确,这样我会获得一些魔力:D
    【解决方案2】:

    我相信它会更像:

    def destroy
      @calendar = Calendar.find(params[:id])
      calendar_admin_role = @calendar.administrations.role
      if @calendar.destroy
        flash[:success] = "Calendar deleted"
        current_user.total_calendar_count -= 1
        if calendar_admin_role == "Owner"
          current_user.owned_calendar_count -= 1
        end
      end
      redirect_to dashboard_path
    end
    

    但是经过一整天的工作,这让我无法想象,所以可能是错误的。

    【讨论】:

    • 好的,谢谢。仅供参考,为什么在action 方法中,我们使用if @calendar.save 而不是“如果@calendar.create, but in the destroy”方法,我们使用if @calendar.destroy 而不是if @calendar.delete。抱歉,如果这是一个愚蠢的问题,我对 Ruby 和 Rails 还很陌生,对此我有点困惑。
    • 在这种情况下,@calendar = Calendar.build(calendar_params) 可能是更多的“Rails”,然后是 if @calendar.savebuild 不会在数据库中创建任何东西,save 会这样做。 deletedestroy 不同。它删除对象而不运行它们的回调。见stackoverflow.com/questions/22757450/…
    猜你喜欢
    • 1970-01-01
    • 2022-08-18
    • 2011-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多