Flash message not shown after redirect_to

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:

<%= flash[:notice] %>
<%= @content_for_layout %>
...

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

Is the layout your using for the www.localhost.net:3001 the
application.rhtml?

Hi Ehud,

Ehud R. 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

Bill W. wrote:

Hi Ehud,

Ehud R. 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 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

On 4/4/07, Bill W. [email protected] 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.

Pat

Hi Ehud,

Ehud R. 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.

Pat M. wrote:

On 4/4/07, Bill W. [email protected] 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.

Pat

Hi Pat,
Your suggestion makes a lot of sense (I also read the part about in the
the Agile Web D. 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

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