Forum: Ruby on Rails How to: Prevent deletes and report

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James B. (Guest)
on 2009-02-17 23:23
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.
This topic is locked and can not be replied to.