4
  get '/test' do
     session[:my_session_id] = generate_random_id()
     puts 'begin haml debug'
     haml :"static/haml_page", :locals=>{:session_id => session[:my_session_id]}
  end

I see in a log that a page above is constantly called twice:

begin haml debug
127.0.0.1 - - [02/Nov/2012 00:00:01] "GET / HTTP/1.1" 200 4317 1.5421
127.0.0.1 - - [02/Nov/2012 00:00:01] "GET /js/base/jquery.pjax.002902.js HTTP/1.1" 304 - 0.0234
[2012-11-02 00:00:01] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true
127.0.0.1 - - [02/Nov/2012 00:00:01] "GET /css/docs.002902.css HTTP/1.1" 200 165 0.1086
.................................
begin haml debug
127.0.0.1 - - [02/Nov/2012 00:00:04] "GET / HTTP/1.1" 200 4317 1.9288

It makes me have some issues. Why is this happening?

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205
  • 1
    Does it happen when you try it with a different browser, or perhaps curl? e.g. `curl http://localhost:9292` – ian Nov 01 '12 at 18:06
  • No, it doesn't! Chrome requests it twice. Why? – Alan Coromano Nov 01 '12 at 18:42
  • Your route says `get '/test'`, but your log says `GET /`. have you changed the example from the real thing? – matt Nov 01 '12 at 19:02
  • yes, this is just a typo. Opera doesn't request a page twice. – Alan Coromano Nov 01 '12 at 19:07
  • If it's being requested twice by Chrome then try another Webkit browser like Safari and see if it does it there too. I've no idea why Chrome would do that, but at least you're narrowing it down. – ian Nov 01 '12 at 21:13
  • you could also try another server like thin, puma or webrick. – three Nov 02 '12 at 08:09
  • no client side scripts having fun? (had some fun problems myself which turned out to be caused by including the same script twice....) – froderik Nov 03 '12 at 13:55

2 Answers2

2

I've moved to Puma server insted of Webrick because of similar issues. Unfortunately I've lost example code with this problem. In any case, if you have such problems please learn what brouser does:

  1. Developers tool > Network (tab) will show exact sourse of request if it exists
  2. Try to narrow this issue/bug by reducing code i.e. comment all JavaScripts, change page contents to 'Hello Wold' and observe is problem still happens
  3. Share your code:)

Sorry for posting here, I don't know how to post this as addition to your qestion.

sashaegorov
  • 1,821
  • 20
  • 26
0

This is a hack, but if you really need to get it to only run the code once: Create a global boolean variable. in the route, wrap everything in a conditional on the boolean. if false, set it true, run your code, and set it false again.