How to: Prevent deletes and report


#1

I have a model that may not have its entries deleted. I wish to have
all attempts to delete records raise a notification / error, report this
to the user, and return to the original page.

In my model file I put this:

def destroy
errors.add_to_base(“Deletion prohibited!”)
end

In the controller I have this:

DELETE /entities/1

DELETE /entities/1.xml

def destroy
@entity = Entity.find(params[:id])

respond_to do |format|
  if @entity.destroy
    flash[:notice] = 'Entity was destroyed.'
    format.html { redirect_to(entities_url) }
    format.xml  { head :ok }
  else
    format.html { redirect_to(entities_url) }
    format.xml  { render :xml => @entity.errors,
                         :status => :unprocessable_entity }
  end
end

end

This seems to work insofar as the records are not deleted. But the
error message is not displayed. What am I missing?

I know, just get rid of the delete link in the view. Eventually, yes,
right now I want to assure myself that no matter how the request comes
in the action is disallowed and an error is raised. I will probable
want to log the attempt as well.