2013-03-15 81 views
0

如何設置響應以便動態生成的json數據將被瀏覽器下載,而不是顯示?Rails渲染JSON - 如何下載響應?

我使用主動聯繫

member_action :download, :method => :get do 
    application = Application.find(params[:id]) 
    respond_to do |format| 
     format.json { render json: application.to_json(include: { categories: { include: { my_cases: { include: [{ questions: { include: :answers } }, :keys ] } } } }) } 
    end 
    end 

回答

4

使用send_data方法:

respond_to do |format| 
    format.json { send_data application.to_json(include: { categories: { include: { my_cases: { include: [{ questions: { include: :answers } }, :keys ] } } } }), type: :json, disposition: "attachment" } 
end