Forum: Ruby on Rails flash[] not shown

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bd3a4af368439803aa37d318f9941a9f?d=identicon&s=25 David (Guest)
on 2006-05-02 14:36
I am using the following code (inspired by one of the books) to check if
the user's session should have timed out.

  # Check to see if the user has been inactive for longer than the
  # expiry period.  If they have, reset the session.
  def check_timeout
    if session[:expires_at] != nil
      @time_left = (session[:expires_at] - Time.now).to_i
      unless @time_left > 0
        reset_session
        # TODO - This flash is never shown, because we redirect maybe?
        flash[:error] = 'Your session has timed out, please login to
continue.'
        redirect_to :controller => 'security', :action => 'login' and
return false
      end
    end
  end

However, the flash is never shown.  The view includes the correct code
and a flash is shown if the user enters an incorrect username/password.
At one point, the flash did work (before I changed some code) but I have
no idea what actually broke it.

The same happens for the logout event, which simply calls reset_session,
sets the flash then redirects as above (in that order, so I'm not wiping
the flash out from the session I hope).

Is there any reason that the flash wouldn't be shown?

Cheers.
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-05-03 18:59
(Received via mailing list)
I don't know if it's relevant or not, but when i implemented the
'lightning
fast javascript auto completion' recipe, which involved using a
controller
action to load a javascript file, that action ate my flash before it got
where it was supposed to go - the next 'real' action.

I had to put a flash.keep statement in that action so that the contents
would survive til the next real load.
36df5a25b0f7e1a39826370b91497167?d=identicon&s=25 Charles Leeds (Guest)
on 2006-05-03 18:59
(Received via mailing list)
how to handle multiple flash keys (See related posts)
posted by Scott Raymond on rails list

I generally like to add something like this to my application_helper.rb:

def flash_div *keys
  keys.collect { |key| content_tag(:div, flash[key],
                                   :class => "flash_#{key}") if
flash[key] }.join
end



...and then this in the <body> section of layouts/application.rhtml:

<%= flash_div :warning, :notice %>



Now, if my controller puts anything into flash[:warning] or
flash[:notice],
they'll render like:

<div class="flash_warning">Warning here</div>
<div class="flash_notice">Notice here</div>


Handling the CSS styling

.flash_notice {
    color: green;
    font-size: 8pt;
}
This topic is locked and can not be replied to.