I tried to export a csv file in my Rails app.
It works fine locally and if will return a csv file with data. However when push to production, it return a csv file which is empty.
Is there any possible reason about this problem?
View:
<%= link_to "Export Places", {controller: "admin/neighborhoods", action: "export_csv", format: "csv"}, 'data-no-turbolink' => true, :class => "button" %>
Route:
get 'admin_neighborhoods_export' => 'admin/neighborhoods#export_csv'
Controller:
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
end
Modal:
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
end
Have found the reason.
There is a bad data in production db. So one error happens when I use 'find' to search a row in db.
NeighborhoodBoundariesMaponics.find(neighborhood.gid)
Now I change to use 'where' and it works.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.