Rails 4: how to check if an instance has been deleted -


in our rails app, have 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 create action, when new @calendar created, run @calendar.save check if new instance has been created, , perform actions.

we implement similar process in our destroy action.

we thinking of updating destroy method 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 syntax of code correct, in particular if @calendar.delete , if @calendar.administrations.role == "owner"?

and, importantly, code of destroy action make sense?

i believe more like:

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 

but off top of head after long day @ work wrong.


Comments