Can't send flash message from Model method
- by Andy
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!