rails export csv在生产中返回空,在本地工作正常

时间:2015-08-26 09:04:49

标签: ruby-on-rails csv

我尝试在我的Rails应用程序中导出csv文件。

它在本地工作正常,如果将返回带有数据的csv文件。但是,当推送到生产时,它会返回一个空的csv文件。

这个问题有什么可能的原因吗?

查看:

<%= link_to "Export Places", {controller: "admin/neighborhoods", action: "export_csv", format: "csv"}, 'data-no-turbolink' => true, :class => "button" %>

路线:

get 'admin_neighborhoods_export' => 'admin/neighborhoods#export_csv'

控制器:

def export_csv
@neighborhoods = Neighborhood.order(:created_at)
time = Time.now.strftime("%Y%m%d%H%M%S").to_s
respond_to do |format|
  format.html
  format.csv do
    headers['Content-Disposition'] = "attachment; filename=\"places_export_#{time}.csv\""
    headers['Content-Type'] ||= 'text/csv'
    send_data(Neighborhood.to_csv(@neighborhoods), :type => "text/csv", :filename => "places_export_#{time}.csv")
  end
end

模态:

def self.to_csv(neighborhoods)
CSV.generate(headers: true) do |csv|
  nbh_columns = []
  nbh_columns.concat(column_names)
  nbh_columns.concat(["maponics_neighborhood", "maponics_city", "maponics_state"])
  csv << nbh_columns
  neighborhoods.each do |neighborhood|
    values = []
    values.concat(neighborhood.attributes.values_at(*column_names))
    if neighborhood.gid.present?
      nbh_maponic = NeighborhoodBoundariesMaponics.find(neighborhood.gid)
      values.concat([nbh_maponic.neighborhd, nbh_maponic.city, nbh_maponic.state])
    else
      values.concat(["", "", ""])
    end
    csv << values
  end
end

1 个答案:

答案 0 :(得分:0)

找到了原因。

生产数据库中存在错误数据。因此,当我使用'find'搜索db中的行时会发生一个错误。

NeighborhoodBoundariesMaponics.find(neighborhood.gid)

现在我改为使用'where'并且它有效。