I have a Rails 3.2 app that manages students. It has a fairly typical
nav
bar across the top (Foundation 5) which contains a quick search field.
The
nav bar is displayed on every page of the site.
If you enter a valid (numeric) student ID into the search field, you
simply
jump to that student’s page. If you enter text or other non-numeric
input,
you get a flash error asking for valid input. If you enter an id that’s
not
found, you get a flash notice saying it wasn’t found. In either of the
latter two cases, the controller should just drop you back to whatever
page
you came from and display the appropriate flash message.
For starters, here’s the search field in the view:
<%= form_tag search_students_path, method: ‘get’ do %>
And here’s the controller action:
def search
session[:return_to] ||= request.referer
if params[:search].to_i.zero?
flash[:error] = %Q[ Please enter a
numeric student ID.].html_safe
redirect_to session.delete(:return_to)
else
id = params[:search].to_i.abs
@student = Student.search(id).first
if @student
redirect_to @student
else
flash[:caution] = %Q[ Sorry, we
couldn’t find a student with ID #{id}.].html_safe
redirect_to session.delete(:return_to)
end
end
end
Lastly, here’s the code for rendering flash messages in
application.html.erb:
<% flash.each do |key, value| %>
In Chrome and FireFox this works exactly as expected. The flash appears
for
one request, then disappears. However, in Safari, once the flash comes
up
it never goes away for that page. So if you get the error flash on the
home
page, for example, you can refresh all you want. It stays put. You can
go
to another page, and then come back, and it’s still there. The same is
true
for other pages. Once the flash message has appeared on a given page, it
doesn’t go away.
Thus my question: how can I get Safari to clear the flash after the
first
request?
I’m aware of the whole “flash vs. flash.now” issue when rendering pages.
But even then, the flash will disappear if you simply refresh. I
actually
tried flash.now in this case, but then the flash isn’t displayed at all
in
any browser.
Since this appears to be a browser-specific problem, here are some
further
stats on my system:
- Mac OS X 10.9
- Safari 7.0
- Rails 3.2.16
One final observation. After playing around with this issue in Safari, I
noticed that if I clicked my bookmark for http://localhost:3000/, that
would clear the flash. Of course, all the navigation links in my site
layout contain relative paths, whereas the bookmark is calling a full
url.
Anyway, hope that made sense. Thanks in advance for your help!