Flashing notice from a model throws

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?


def destroy_user
if request.post?
user = User.find(params[:id])
flash[:notice] = “user has been deleted!”
redirect_to(:action => :list_users)

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”)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs