2013-03-15 73 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