2011-02-18 2 views
4

Ruby on Rails 3을 사용하고 있으며 JSON/XML 응답의 값을 설정하려고합니다. 내가 JSON/XML에 대한 HTTP GET 요청을 할 때 내 컨트롤러에서'format.json/xml {render : json/xml => @ user.to_json/xml}'에서 JSON/XML 응답의 헤더를 설정하는 방법은 무엇입니까?

내가

respond_to do |format| 
    format.xml { render :xml => @user.to_xml } 
    format.json { render :json => @user.to_json } 
end 

을 가지고, 내가/세트를 추가하고자하는 이들

header: 
    date: 
    - Fri, 18 Feb 2011 18:02:55 GMT 
    server: 
    - Apache ... 
    etag: 
    - "\"0dbfd0ec23934921144bd57d383db443\"" 
    cache-control: 
    - max-age=0, private, must-revalidate 
    x-ua-compatible: 
    - IE=Edge 
    x-runtime: 
    - "0.033209" 
    status: 
    - "200" 
    transfer-encoding: 
    - chunked 
    content-type: 
    - application/json; charset=utf-8 #or application/xml; charset=utf-8 
http_version: "1.1" 
message: OK 
read: true 

같은 일반적인 값을 설정한다 header 값을 입력하고 message2 또는 header2과 같은 새 매개 변수를 추가하십시오.

format.json/xml { render :json/xml => @user.to_json/xml } 구문으로 어떻게 할 수 있습니까?

답변

8

format.foo { render ... } 것은 블록을 취합니다. 거기에 원하는 것을 넣을 수 있습니다.

format.json do 
    response['X-Message-1'] = 'Hello' 
    render :json => @user.to_json 
end 
관련 문제