所以我有兴趣编写一种方法,当我单击导出按钮时,数据库中的某些数据导出为 JSON 数据。
我该怎么做?
程序是这样的
get data from db -> convert to json -> write to file -> save file to computer.
谢谢
最佳答案
您可以轻松地使用 to_json 方法,send_data 和配置 header :
应用程序/ Controller /application_controller.rb
class ApplicationController < ActionController::Base
def users_json
data = User.all.to_json
send_data data, :type => 'application/json; header=present', :disposition => "attachment; filename=users.json"
end
end
关于ruby-on-rails - 如何在 Rails 4.2 中添加 JSON 文件导出功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31927229/