2

In my app

user can access to the community page just like this
http://example.com/communities?tag=lovely

then if I try to go back to the previous page. I always get this message

Are you sure you want to send a form again

This only appears when it's accessed from Safari

How can I avoid that. I don't want user to see it every time when they go back.

routes.rb

resources :communities
MKK
  • 2,713
  • 5
  • 31
  • 51
  • So, you're going back to a page that was generated in response to your posted form. This might not be desirable in, say, online banking systems. That's why the browser asks you. "How can I avoid that?" - mm, don't go back to such pages? :) – Sergio Tulentsev Feb 01 '13 at 19:12
  • @SergioTulentsev Thanks for a reply. So is this how it's supposed to be? when you had tag search system. would your application become the same way just like mine? – MKK Feb 01 '13 at 19:18
  • Instead of posting a form, you could replace a `window.location` by javascript, thus turning it into GET-request – Sergio Tulentsev Feb 01 '13 at 19:21
  • @SergioTulentsev Could you please show me how to? – MKK Feb 01 '13 at 19:32

2 Answers2

2

One way to avoid this problem is to make your POST responses return a redirect (HTTP 301 or 303) to a new page, which the browser then requests with GET. Going back should (though doesn't always) fetch the original form page, not the POST.

In Rails, this run-around often looks like this:

  1. client GETs /foos/new
  2. server returns HTML with <FORM method='POST' action='/foos'>
  3. client POSTs /foos (with params)
  4. server creates a new Foo with id 123
  5. server returns redirect to /foos/123
  6. client GETs /foos/123
  7. server returns HTML for newly created Foo
Community
  • 1
  • 1
AlexChaffee
  • 8,092
  • 2
  • 49
  • 55
  • Thanks for a reply. for now my output code is just like this `respond_to do |format| format.html # index.html.erb format.json { render json: @communities } end` – MKK Feb 01 '13 at 19:31
  • Yeah, that sounds right. Odd that the boilerplate Rails code doesn't handle this already. – AlexChaffee Feb 01 '13 at 19:44
  • Looking at your question code again, maybe the problem is that you are using POST to fetch `/communities?tag=lovely` when you should be using GET. POST is for when you want to create or update data on the server; GET is for reading what's already there. – AlexChaffee Feb 01 '13 at 19:47
  • simply how can I fix this? should I add `get 'communities/tag=:tag' => 'communities#index', :via => :get, :as => :communities_tag` – MKK Feb 01 '13 at 19:50
  • I believe your problem is not with routing, but with the client code (HTML or JS) on the requesting page. Rails `resources :communities` should provide the right route to your `def index` method and pass you the `tag` parameter in the `params` array. – AlexChaffee Feb 01 '13 at 19:53
  • `def show` won't be matter here. the url transition is just like this. A user is at example.com/communities then if he clicks particular tag, it takes him to example.com/communities?tag=lovely – MKK Feb 01 '13 at 19:55
-1

just add

window.location.replace("about:blank");

Rosch
  • 1
  • 1