flash message not shown after redirect_to

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

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.

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.

Pat

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