2012-06-21 50 views
1

我有一個簡單的開箱滑軌服務器運行,我想弄清楚如何使它與If-Modified-Since標題一起工作。我正在使用以下curl請求。Rails 3 HTTP-header If-Modified-Since使用curl

捲曲-I本地主機:3000 /表演/ 1 --header '如果-Modified-Since的:週四2012年6月21日19點16分二十秒GMT'

我總是得到200 OK,但我想304不改性。

當我通過瀏覽器做到這一點時,我得到了304未修改,但沒有捲曲。

我做錯了嗎?

UPDATE

如果我運行

捲曲-v -I本地主機:3000 /顯示/ 1 --header '的If-Modified-時間:星期三,2012年6月21日19時16分二十秒GMT'

我得到:

* About to connect() to localhost port 3000 (#0) 
* Trying 127.0.0.1... connected 
* Connected to localhost (127.0.0.1) port 3000 (#0) 
> HEAD /shows/1 HTTP/1.1 
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 
> Host: localhost:3000 
> Accept: */* 
> If-Modified-Since: Thu, 21 Jun 2012 19:16:20 GMT 
> 
< HTTP/1.1 200 OK 
< Content-Type: text/html; charset=utf-8 
< X-Ua-Compatible: IE=Edge 
< Etag: "35ec119e3b6c7ccde9eeee82afcd4ee9" 
< Cache-Control: max-age=0, private, must-revalidate 
< X-Request-Id: dd365eb0b80f5b9ff62bc16f3dbbe494 
< X-Runtime: 0.007800 
< Content-Length: 0 
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-02-16) 
< Date: Thu, 21 Jun 2012 19:37:55 GMT 
< Connection: Keep-Alive 
< Set-Cookie: _spacebarfm-  rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWY0NzNjYmFlYzJhNzgzZWRjMjc2MmU4YWFkZDc1OTIwBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMWhjU3NvQ2VuRkR1NmJRREV1SmxOMUlaK3VhQU0ycjY2am94cFFmOTVnTTg9BjsARg%3D%3D--f9c756d08065c423cdecba2d710b1b74bf798c12; path=/; HttpOnly 

的Set-Cookie:_spacebarfm-rails_session = BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJWY0NzNjYmFlYzJhNzgzZWRjMjc2MmU4YWFkZDc1OTIwBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMWhjU3NvQ2VuRkR1NmJRREV1SmxOMUlaK3 VhQU0ycjY2am94cFFmOTVnTTg9BjsARg%3D%3D - f9c756d08065c423cdecba2d710b1b74bf798c12;路徑= /;僅Http

* Connection #0 to host localhost left intact 
* Closing connection #0 

的控制器Rails代碼是:

def index 
    @shows = Show.all 

    respond_to do |format| 
    format.html # index.html.erb 
    format.json { render json: @shows } 
    end 
end 

的鉻輸出如下:

Request URL:http://localhost:3000/shows/1.json 
Request Method:GET 
Status Code:304 Not Modified 
Request Headersview source 
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Cookie:_btest_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTNkNzI3ODc1NjU0M2EyZTViOWY0ZTgzMjU0M2IzYmY4BjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMUFVZVN0ZHp6L0JRNGVMZDZST2JSNG54Zlg4T3VmSnk4RFNhWXRHbmljK3c9BjsARg%3D%3D--ceb783671372cbcc3ecf971340f2a2e2424c9620; _spacebarfm-rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTNhMTcxNjMyNzJkMWRjYzAxY2NiN2EwN2JjMzE5YWFjBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTZZREp5ZC92VTlEQk9oKytCK0hKYW5na1M4bzRPRUM3U3lmWURzU09neWs9BjsARg%3D%3D--793b15b38511bd0805df3a210d7e72152905b8b9 
Host:localhost:3000 
If-None-Match:"bb012b3a16e7e80ec271b0d234a9b8ce" 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5 
Response Headersview source 
Cache-Control:max-age=0, private, must-revalidate 
Connection:close 
Date:Thu, 21 Jun 2012 21:09:28 GMT 
Etag:"bb012b3a16e7e80ec271b0d234a9b8ce" 
Server:WEBrick/1.3.1 (Ruby/1.9.3/2012-02-16) 
Set-Cookie:_spacebarfm-rails_session=BAh7B0kiD3Nlc3Npb25faWQGOgZFRkkiJTNhMTcxNjMyNzJkMWRjYzAxY2NiN2EwN2JjMzE5YWFjBjsAVEkiEF9jc3JmX3Rva2VuBjsARkkiMTZZREp5ZC92VTlEQk9oKytCK0hKYW5na1M4bzRPRUM3U3lmWURzU09neWs9BjsARg%3D%3D--793b15b38511bd0805df3a210d7e72152905b8b9; path=/; HttpOnly 
X-Request-Id:2435168fa20699508e9282c37566c015 
X-Runtime:0.005378 
X-Ua-Compatible:IE=Edge 
+0

使用'-v'運行'curl'並粘貼輸出,粘貼瀏覽器的輸出(如果您使用的是瀏覽器,只需打開Web工具並檢查請求 - 響應)。 –

+0

請發佈您的ShowsController代碼,特別是show動作。 –

+0

我剛剛加了 – JonathanC

回答

4

您需要使用陳舊?方法來告訴Rails你想使用if-modified-

你實際上正在調用show方法。將其更改爲:

def show 
    @show = Show.find(params[:id]) 

    if stale?(:etag => @show, :last_modified => @show.updated_at.utc) 
    respond_to do |format| 
     format.html # index.html.erb 
     format.json { render json: @show } 
    end 
    end 
end 
+0

謝謝!這就是我一直在尋找的。 – JonathanC