- nkf
- csb gem
Last active
November 9, 2021 07:03
-
-
Save taketo1113/ab2733461fb3f48427ec17f4a450af5e to your computer and use it in GitHub Desktop.
Output csv file with Shift JIS using csb gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/controllers/reports_controller.rb | |
require 'nkf' | |
class ReportsController < ApplicationController | |
def index | |
@reports = Report.preload(:categories) | |
# for csv | |
@version = Time.now.strftime("%Y%m%d%H%M%S") | |
@filename = "reports-#{@version}.csv" | |
respond_to do |format| | |
format.csv do | |
csv = Csb::Builder.new(items: @reports) | |
csv.cols.copy!(Report.csb_cols) | |
csv_data = csv.build | |
send_data(NKF::nkf('-Ws', csv_data), filename: @filename, type: 'application/x-csv;charset=Shift_JIS') | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment