Rails渲染json - 如何下载响应?

时间:2013-03-15 16:59:27

标签: ruby-on-rails activeadmin

如何设置响应以便浏览器下载动态生成的json数据,而不显示?

我正在使用Active Admin

  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

1 个答案:

答案 0 :(得分:6)

使用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