Can't send flash message from Model method

Posted by Andy on Stack Overflow See other posts from Stack Overflow or by Andy
Published on 2010-06-07T17:36:09Z Indexed on 2010/06/07 17:42 UTC
Read the original article Hit count: 221

Filed under:
|
|

Hello, I'm trying to prevent a record that has a relationship to another record from being deleted. I can stop the deletion but not send a flash message as I had hoped!

class Purchaseitem < ActiveRecord::Base
  before_destroy :check_if_ingredient

  ...

  def check_if_ingredient
    i = Ingredient.find(:all, :conditions => "purchaseitem_id = #{self.id}")
      if i.length > 0
        self.errors.add(:name)
      flash.now[:notice] = 
        "#{self.name} is in use as an ingredient and cannot be deleted"
      return false
    end
  end

This will prevent a the delete wihthout the flash line, and when I add it I get:

undefined local variable or method `flash' for #

Any help would be much appreciated!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about model