Question

I have a searchform which displays available rooms to book, if i book a room and hit the back button in my browser afterwards, the searchresults are still displayed. Is it possible to reload the page when i hit back, so that the user has to click on the searchbutton again to see the available rooms? I just want the default searchform without the results? I tried this before:

before_filter :set_cache_buster
def set_cache_buster
    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
 end

but this just reloads the page with the users input paramteres still in the search, so it basically shows the same page as without reloading, thanks

Was it helpful?

Solution

If your form method is "GET", the page is always available when you hit back button and the query strings are all in url. In many cases this effect is preferred, for example you can bookmark a Google search url.

You can change form method as "POST" so the page will not be available again.

Add

Use this with a bit caution. In convention "GET" is meant to get data and "POST" is meant to add some data. A search form is built for get data so the convention of form method is "GET", nothing wrong here. So only change it if you really need to that.

Add more OP said he can't change form method

Well, here is a hack, if you want search result to appear to only on click of button, instead of url with params.

The idea is to use Javascript to attach a new hidden input when clicking button, then controller verify if this new param exists. If exists, process and return result, else show some error.

# JavaScript
$('#the_form button').on('submit', function() {
  this.preventDefault();
  this.parent.append('<input type="hidden" name="human_hit" value="yes" />');
  this.parent.submit();
})

# Controller
def search
  if params[:human_hit]
    # Do search logic and deliver
  else
    redirect_to :back, alert: "You need to hit the button to search"
  end
end

This is a hack and not verified, the idea should be able to do the trick.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top