Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4:如何检查实例是否已被删除_Ruby On Rails_Ruby On Rails 4_Model View Controller_Destroy - Fatal编程技术网

Ruby on rails Rails 4:如何检查实例是否已被删除

Ruby on rails Rails 4:如何检查实例是否已被删除,ruby-on-rails,ruby-on-rails-4,model-view-controller,destroy,Ruby On Rails,Ruby On Rails 4,Model View Controller,Destroy,在Rails应用程序中,我们有一个日历控制器: class CalendarsController < ApplicationController def create @calendar = current_user.calendars.create(calendar_params) current_user.add_calendar_and_role(@calendar.id, 'Owner') if @calendar.save curren

在Rails应用程序中,我们有一个
日历控制器

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
此代码的语法是否正确,尤其是
if@calendar.delete
if@calendar.administrations.role==“Owner”


而且,最重要的是,这个
销毁
行动的代码有意义吗?

我相信它更像是:

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

但是,在工作了一整天之后,这已经超出了我的想象,所以可能是错误的。

你想过使用
持久化方法吗?
方法

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

好的,谢谢。仅供我参考,为什么在
action
方法中,我们使用
if@calendar.save
而不是``if@calendar.create
,但是在
destroy`方法中,我们使用
if@calendar.destroy
而不是
if@calendar.delete
。很抱歉,如果这是一个愚蠢的问题,我对Ruby&Rails还很陌生,对此我有点困惑。在这种情况下,使用
@calendar=calendar.build(calendar\u params)
,可能更像是“Rails”,然后使用
if@calendar.save
build
不会在数据库中创建任何内容,
save
会这样做<代码>删除
销毁
不同。它删除对象而不运行它们的回调。看,不,我没有。其他代码结构的优点是什么?这是Rails的方法吗?坚持?检查您的记录是否仍在数据库中或仅在内存中。如果它没有在数据库中“持久化”,它将返回“false”。这就是你想做的。这是一种Rails ActiveRecord方法:听起来确实像我们想要实现的,非常感谢。没问题!请检查我的答案是否正确,这样我将获得一些魔力:D
@calendar.destroy
unless @calendar.persisted?
   ... some code here ....
end