ActionController::Renderers.add :csv do |obj, options|
filename = options[:filename] || 'data'
str = obj.respond_to?(:to_csv) ? obj.to_csv : obj.to_s
send_data str, type: Mime::CSV,
disposition: "attachment; filename=#{filename}.csv"
end
Mime::Type.register "application/csv", :csv
def show
@csvable = Csvable.find(params[:id])
respond_to do |format|
format.html
# 对应这里的 format.csv
format.csv { render csv: @csvable, filename: @csvable.name }
end
end
ActionController::Renderers.remove(:csv)
use_renderer & use_renderers
ActionController::Renderers::RENDERERS
=> #<Set: {:json, :js, :xml}>