Rails 4:如何检查实例是否被删除
Rails 4: how to check if an instance has been deleted
在我们的 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.delete
和 if @calendar.administrations.role == "Owner"
?
而且,最重要的是,这个 destroy
操作的代码是否有意义?
我相信它会更像:
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
但经过漫长的一天工作后,这不是我的头等大事,所以可能是错误的。
您是否考虑过使用 persisted?
方法
@calendar.destroy
unless @calendar.persisted?
... some code here ....
end
在我们的 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.delete
和 if @calendar.administrations.role == "Owner"
?
而且,最重要的是,这个 destroy
操作的代码是否有意义?
我相信它会更像:
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
但经过漫长的一天工作后,这不是我的头等大事,所以可能是错误的。
您是否考虑过使用 persisted?
方法
@calendar.destroy
unless @calendar.persisted?
... some code here ....
end