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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Flashs don''t work on models, only passing from the controllers to the views. You may wanna rethink your logic. -- Thiago Jackiw http://www.railsfreaks.com On May 7, 1:18 pm, wickNbomb <chadalder...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ah OK thanks On May 7, 1:38 pm, Thiago Jackiw <tjac...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Flashs don''t work on models, only passing from the controllers to the > views. You may wanna rethink your logic. > > -- > Thiago Jackiwhttp://www.railsfreaks.com > > On May 7, 1:18 pm, wickNbomb <chadalder...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
wickNbomb wrote:> "undefined local variable or method `flash'' for #<User:0x28fd3f4>"As has been pointed out, the "flash" is related to the HTTP session and models should work independently of this so models cannot access flash, session, etc.> user.destroyCheck the result of the destroy. If your exception was raised, then the destroy will fail. You can catch that here and update the flash accordingly. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---