Hi everyone... I''m new to rails and trying to set up error messages via the flash mechanism. I have method in my application.rb file that tries to find the current subdomain the user is accessing (eg: ehud.localhost.com:3000 => ehud) for later use. If a user with the requested subdomain does not exist a redirect is sent to the main site index and a flash message is supposed to be written. The redirect goes through fine, but the flash message is not shown. code follows: application.rb: class ApplicationController < ActionController::Base include AuthenticatedSystem before_filter :find_user protected def find_user # if we are not trying to access the main www site, try to extract # the user profile if (request.subdomains.first != ''www'' && request.subdomains.first !''WWW'') if !session[:currentsite].nil? @current_user_profile = session[:currentsite] else @current_user_profile User.find_by_subdomain(request.subdomains.first) if (!@current_user_profile.nil?) session[:currentsite] = @current_user_profile else logger.error("Attempt to access invalid user profile " + request.subdomains.first) flash[:notice] = "user " + request.subdomains.first + " does not exist" redirect_to("http://www.localhost.net:3001/") end end end end end ----------- application.rhtml: ... <div id="ContentContainer"> <div id="notice"><%= flash[:notice] %></div> <%= @content_for_layout %> </div> ... ----------- does it have something to do with redirecting to a specific url instead of an action? or maybe with the fact the the flash message is generated in the before_filter method which is also called again for the redirect itself? any help would be appreciated here! thanks, Ehud -- 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 -~----------~----~----~----~------~----~------~--~---
Is the layout your using for the www.localhost.net:3001 the application.rhtml? -- 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 -~----------~----~----~----~------~----~------~--~---
Hi Ehud, Ehud Rosenberg wrote:> If a user with the requested subdomain does not exist a redirect is sent > to the main site index and a flash message is supposed to be written. > The redirect goes through fine, but the flash message is not shown.You''re dealing with some Rails fundamentals. A Rails application exists for exactly one request/response cycle. The only thing that lives on is session data or data stored in the database. A redirect completes a request/response cycle and starts a new one. That''s why your flash message isn''t making it. You''ll need to store it, probably in a session variable, and then retrieve it in the controller method you''re redirecting to. hth, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Bill Walton wrote:> Hi Ehud, > > Ehud Rosenberg wrote: > >> If a user with the requested subdomain does not exist a redirect is sent >> to the main site index and a flash message is supposed to be written. >> The redirect goes through fine, but the flash message is not shown. > > You''re dealing with some Rails fundamentals. A Rails application exists > for > exactly one request/response cycle. The only thing that lives on is > session > data or data stored in the database. A redirect completes a > request/response cycle and starts a new one. That''s why your flash > message > isn''t making it. You''ll need to store it, probably in a session > variable, > and then retrieve it in the controller method you''re redirecting to. > > hth, > BillHi bill, thanks for the quick response. However, I''m not sure I got it... isn''t that exactly what flash is for? to store a message for the next redirect and then be deleted automatically? If not, what''s the difference between what i''m doing and the common way of doing it? Thanks again, Ehud -- 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 -~----------~----~----~----~------~----~------~--~---
Hi Ehud, Ehud Rosenberg wrote:> Hi bill, > thanks for the quick response. > However, I''m not sure I got it... isn''t that exactly > what flash is for? to store a message for the next > redirect and then be deleted automatically?You''re right. I just flipped to p.322 in AWDwR (v1) and it says "values stored into the flash during the processing of a request will be available during the processing of the immediately following request." My bad. I''ve only used flash for storing a message for rendering in the current request/response cycle and didn''t remember that. So it will live through the redirect. There''s also a note at the bottom of the page that emphasizes the ''following request'' aspect.> If not, what''s the difference between what i''m doing > and the common way of doing it?After taking another look at your code in that light, I think the answer may lie in the fact that a) your redirect_to is occurring within application.rb, or possibly b) the redirect is within / between separate domains. I don''t say this with any authority, but I would speculate that either the Rails internals involved in invoking code in application.rb or the domain switching may somehow trigger the "end of request" that causes the flash to disappear. If I had the time, I''d put together a sandbox app to dig into it. Unfortunately, I don''t at the moment. If you do, I hope you''ll let us know what you find out. If you don''t, you might think about storing the message in a session variable before you do the redirect, and then check / access it in your main page. If it exists, you know you put it there and why. Since it''s in a session variable, you don'' t need to worry about finding the right message for that user. It''s their session. It''s their message. hth, Bill PS. and thanks for correcting me. I appreciate it. Best regards. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Ehud, I think I know what''s going on here. Think of redirect_to like a render() call that just sends a response which causes the browser to request the page you passed to redirect_to. There are 3 request/response cycles involved here. You are putting something in the flash in one action which then renders a page (cycle 1), then the user clicks a link on that page to an action which redirects (cycle 2). That causes the browser to request, and the server to deliver, another page (cycle 3). The values you put in the flash during cycle 1 will be intact during cycle 2 but they''re gone in cycle 3. Is this what''s goin'' down? Solution: flash.keep() In cycle 2, before calling redirect_to, call flash.keep . . you can pass it a key from the hash if you only want to preserve part of the flash hash. 8-) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
On 4/4/07, Bill Walton <bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> or possibly b) the redirect is within / between separate domains. I don''t > say this with any authorityThe problem is that sessions are stored based on the domain used. If you access foo.example.com and then redirect to bar.example.com, a new session will be created (or it will use an existing bar.example.com session). You solve this by changing the domain used for sessions. At the bottom of config/environments/production.rb, add # Sessions should be shared between subdomains ActionController::Base.session_options[:session_domain] = ".mydomain.com" Now your Rails app will share a user''s session among all the subdomains. That ought to solve your flash program, because flash uses the session to do its thing. Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pat Maddox wrote:> On 4/4/07, Bill Walton <bill.walton-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote: >> or possibly b) the redirect is within / between separate domains. I don''t >> say this with any authority > > The problem is that sessions are stored based on the domain used. If > you access foo.example.com and then redirect to bar.example.com, a new > session will be created (or it will use an existing bar.example.com > session). > > You solve this by changing the domain used for sessions. At the > bottom of config/environments/production.rb, add > > # Sessions should be shared between subdomains > ActionController::Base.session_options[:session_domain] = > ".mydomain.com" > > Now your Rails app will share a user''s session among all the > subdomains. That ought to solve your flash program, because flash > uses the session to do its thing. > > PatHi Pat, Your suggestion makes a lot of sense (I also read the part about in the the Agile Web Development book now) - However it still does not seem to work... I''ve restarted Apache just to make, but still no luck. Should I also include port number to the domain name? (.localhost.com:3001?) Thanks for giving me a push in the right direction, I''m sure I can figure it out now. Ehud -- 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 -~----------~----~----~----~------~----~------~--~---
Thanks, Pat. That''s *very good* info. Probably saved me from asking the ''why isn''t this working'' question in the very near future. Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Cool. I hadn''t come across flash.keep. Thanks, Blake. Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---