Я включил функцию импорта CSV в свое приложение rails. Но проблема, с которой я столкнулся, заключается в том, что если в какой-либо отрасли есть апостроф в заголовке, он странно проявляется в CSV. Как я могу добавить мета http-Equiv = "Content-Type" content = "text / html; charset = ISO-8859-1", чтобы CSV содержал апостроф.
CSV_HEADER = %w[title role team]
def self.to_csv
CSV.generate do |csv|
csv << CSV_HEADER
all.each do |industry|
csv << [
industry.title,
industry.role,
industry.team
]
end
end
end
<%= link_to "Download CSV", admin_path(@industry, :format => :csv, :filterrific => @filterrific.to_hash), class: "btn btn-primary btn-2x" %>
действие контроллера
def application
require 'csv'
add_breadcrumb "Admin", :main_admin_path
add_breadcrumb "Industrial Applications"
@filterrific = initialize_filterrific(
Industry,
params[:filterrific],
select_options: {
with_status: Industry.options_for_status
}
) or return
@industries = @filterrific.find
@industries = @industries.paginate(:page => params[:page], :per_page => 10) unless request.format == 'csv' #does pagination if not csv format
respond_to do |format|
format.html
format.js
format.csv { send_data @industries.to_csv, :type => 'text/csv; charset=iso-8859-1; header=present' }
end
rescue ActiveRecord::RecordNotFound => e
puts "Had to reset filterrific params: #{ e.message }"
redirect_to(reset_filterrific_url(format: :html)) and return
end
Можете ли вы показать действие вашего контроллера?
@engineersmnky добавил действие контроллера
для установки типа контента и т. д. вам необходимо передать параметры методу send_data
apidock.com/rails/ActionController/DataStreaming/send_data
@engineersmnky я добавил: type => 'text / csv; кодировка = iso-8859-1; header = present ', но он по-прежнему показывает странный апостроф в CSV
Вы можете объяснить, что вы имеете в виду под «странным апострофом»?
@engineersmnky Итак, когда я скачиваю csv, Фабрика детских игрушек выглядит вот так в csv Children's Toy Factory. Надеюсь, я смогу объяснить себя
@engineersmnky есть идеи? Я все еще не мог понять
Для решения этой проблемы использовалась концепция BOM (метка порядка байтов).
format.csv { send_data "\uFEFF" + @industries.to_csv, type: :csv }
@engineersmnky извините, это опечатка. это промышленность, а не финансирование.