In our Rails app, we have a 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
In the create
action, when a new @calendar is created, we run @calendar.save
to check if the new instance has actually been created, and then perform some actions.
We would like to implement a similar process in our destroy
action.
We are thinking of updating the destroy
method as follows:
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
Is the syntax of this code correct, in particular if @calendar.delete
and if @calendar.administrations.role == "Owner"
?
And, most importantly, would the code of this destroy
action make sense?