# Flashing notice from a model throws

**URL:** <https://www.ruby-forum.com/t/flashing-notice-from-a-model-throws/136981>\
**Category:** Rails\
**Created:** [May 7, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/flashing-notice-from-a-model-throws/136981 "2008-05-07T22:16:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickNbomb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/e480ec/32.png) [@wickNbomb](https://www.ruby-forum.com/u/wickNbomb)\
**Post date:** [May 7, 2008, 10:16pm UTC](https://www.ruby-forum.com/t/flashing-notice-from-a-model-throws/136981/1 "2008-05-07T22:16:38Z")

</div>

I have a view (list\_users) that calls destroy\_user and I’d like to get  
it to flash an error within the view. The code below works for the  
raise, but if I comment it out and try the 2 lines below it, I get the  
following error:

“undefined local variable or method `flash’ for #User:0x28fd3f4”

Any ideas?

user\_controller.rb:

def destroy\_user  
if request.post?  
user = User.find(params[:id])  
user.destroy  
flash[:notice] = “user has been deleted!”  
end  
redirect\_to(:action =\> :list\_users)  
end

user.rb (model):

def after\_destroy  
if User.count.zero?  
raise “Can’t delete the last user”  
# I’d like it to do this, but can’t seem to make it work:  
# flash.now[:notice] = “Can’t remove the last user”  
# redirect\_to(:action =\> “list\_users”)  
end  
end
