ruby on rails, redirect if hitting back button in browser

AndyB

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

Billy Chan

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.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Browser's back button hidden by unity launcher

분류에서Dev

data not updating while clicking on browser back button

분류에서Dev

Trigger an event on back button in browser/android with javascript

분류에서Dev

Create issue Ruby on Rails의 redirect_to 작업

분류에서Dev

Rails 4 Turbolinks back button and multiple event binding because of pagesCached

분류에서Dev

Simple Form button collection does not save the field - Ruby on Rails

분류에서Dev

After a succesful sign-in when user click on the back button of the browser, "login" page should not appear

분류에서Dev

Rails 4 : redirect_to : back 및 매개 변수 변경 / 삭제

분류에서Dev

rails 5 redirect_back이 작동하지 않습니다.

분류에서Dev

redirect_uri_mismatch. Ruby on Rails를 사용하여 Google에 로그인

분류에서Dev

To @ or not to @ in Ruby on Rails

분류에서Dev

Single page, carousel-style, slide website:Navigate to previous slide, on browser back button, without defaulting to home slide?

분류에서Dev

Disable Links or Prevent Browser Redirect

분류에서Dev

Render and redirect_to in rails

분류에서Dev

Rails redirect_to not rendering

분류에서Dev

"Next" button creates a Viewed_Lesson | Ruby on Rails 4 | Learning App

분류에서Dev

Rails : redirect_to : back이 올바른 로그를 보여도 리디렉션되지 않음

분류에서Dev

Back button not working in Media player

분류에서Dev

To disable back button in popup window

분류에서Dev

Back Button Does Not Stop Handler

분류에서Dev

How to redirect the User From Browser to Application in iOS?

분류에서Dev

Rails redirect_to choice page

분류에서Dev

Rails redirect_to with DELETE method

분류에서Dev

Ruby on Rails 소수?

분류에서Dev

ActiveModel :: ForbiddenAttributesError Ruby On Rails

분류에서Dev

Ruby on Rails Fields For Issue

분류에서Dev

Ruby on rails unitialized constant

분류에서Dev

Each loop in Ruby on Rails

분류에서Dev

Ruby on rails, deactivation of a user

Related 관련 기사

  1. 1

    Browser's back button hidden by unity launcher

  2. 2

    data not updating while clicking on browser back button

  3. 3

    Trigger an event on back button in browser/android with javascript

  4. 4

    Create issue Ruby on Rails의 redirect_to 작업

  5. 5

    Rails 4 Turbolinks back button and multiple event binding because of pagesCached

  6. 6

    Simple Form button collection does not save the field - Ruby on Rails

  7. 7

    After a succesful sign-in when user click on the back button of the browser, "login" page should not appear

  8. 8

    Rails 4 : redirect_to : back 및 매개 변수 변경 / 삭제

  9. 9

    rails 5 redirect_back이 작동하지 않습니다.

  10. 10

    redirect_uri_mismatch. Ruby on Rails를 사용하여 Google에 로그인

  11. 11

    To @ or not to @ in Ruby on Rails

  12. 12

    Single page, carousel-style, slide website:Navigate to previous slide, on browser back button, without defaulting to home slide?

  13. 13

    Disable Links or Prevent Browser Redirect

  14. 14

    Render and redirect_to in rails

  15. 15

    Rails redirect_to not rendering

  16. 16

    "Next" button creates a Viewed_Lesson | Ruby on Rails 4 | Learning App

  17. 17

    Rails : redirect_to : back이 올바른 로그를 보여도 리디렉션되지 않음

  18. 18

    Back button not working in Media player

  19. 19

    To disable back button in popup window

  20. 20

    Back Button Does Not Stop Handler

  21. 21

    How to redirect the User From Browser to Application in iOS?

  22. 22

    Rails redirect_to choice page

  23. 23

    Rails redirect_to with DELETE method

  24. 24

    Ruby on Rails 소수?

  25. 25

    ActiveModel :: ForbiddenAttributesError Ruby On Rails

  26. 26

    Ruby on Rails Fields For Issue

  27. 27

    Ruby on rails unitialized constant

  28. 28

    Each loop in Ruby on Rails

  29. 29

    Ruby on rails, deactivation of a user

뜨겁다태그

보관