Rails - export to csv not loading values - ruby-on-rails

Im doing an export to CSV in Rails everything seems to be ok no errors and the file is being generated, but only the column headers are been exported no other data.
Here is some of the code:
in controller:
#students = Student.where("START_DATE >= ? and END_DATE <= ? and COURSE_NAME like ?", params[:start_date], params[:end_date], params[:course_name]).select('name, course')
(...)
format.csv { send_data #students .to_csv }
in model:
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << column_names
all.each do |valeo|
csv << valeo.attributes.values_at(*column_names)
end
end
end
What I have found as cause of the issue is that when I click on the link
= link_to "Export Records", student_path(:format => :csv)
#students is empty. I don't know why ? or how to solve the issue.

Related

Empty CSV file from rails

I am new to ruby on rails and I am having trouble in exporting the data to csv. I have followed the following video:
https://medium.com/coderaga/rails-import-export-csv-data-without-gem-fbf2a36a84f6
I have put require 'csv' in application.rb file also.
My student.rb model has:
def self.to_csv(fields = column_names, options = {})
CSV.generate(options) do |csv|
csv << fields
all.each do |student|
csv << student.attributes.values_at(*fields)
end
end
end
My students_controller has following:
def index
#students=Student.all
puts #students
respond_to do |format|
format.html
format.csv{send_data #students.to_csv(['UIN', 'Name' ,'Section', 'Attempts', 'Score'])}
end
end
I think the all.each statement is empty as when I am doing puts student.attributes.values_at(*fields), there is nothing so that is why there is nothing being written. But I am not sure how can I fix it.
Student Load (0.3ms) SELECT "students".* FROM "students"
#<Student:0x007f9a2386ddb8>
#<Student:0x007f9a2386dc78>
#<Student:0x007f9a2386db38>
CACHE Student Load (0.0ms) SELECT "students".* FROM "students"
Please help
Try
where.each do |student|
puts student #Debug
csv << student.attributes.values_at(*fields)
end

Rails - Export CSV not working as expected (undefined method)

I am doing a CSV Export in Rails App.
The data is returned as a JSON array from the backend API.
But now I am getting error as "undefined method 'to_csv' for #Hash:0x007f149a5ff978" in the export_data method.
I have included require "csv" in the config/application.rb file, and did server restarts.
Not sure what I am doing wrong.
index.html.erb
<button class="btn btn-default">Export to CSV</button>
usage_controller(front end)
def export_data
rest_resource = RestClient::Resource.new( ENV['USAGE_METRICS_API'] + '/get_events_data', :verify_ssl => false )
request = rest_resource.get :Authorization => cookies.signed[:remember_token], :content_type => 'application/json'
#events_data = JSON.parse(request)
respond_to do |format|
format.html
format.csv { send_data #events_data.to_csv }
end
end
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |event|
csv << event.attributes.values_at(*column_names)
end
end
end
Your self.to_csv method doesn't make sense in the controller
You need to move to_csv to you model which will serialize the data into a csv
follow this tutorial to implement this: http://railscasts.com/episodes/362-exporting-csv-and-excel

How to generate a CSV file in action?

I am trying to generate a CSV output with data from my database. I would like to provide these data to a third party, so I picture I would give to someone a URL (website.com/api_data/cars) and by accessing this URL the person would be able to work with it - I think I want to access the URL and then to see there (in the action) data displayed and separated by , or ;.
But how to do that?
So far, I am trying following approach:
csv_string = CSV.generate do |csv|
cols = ["column one", "column two", "column three"]
csv << cols
csv << ["A", "B", "C"]
#filename = "data-#{Time.now.to_date.to_s}.csv"
end
send_data(csv_string, :type => 'text/csv; charset=utf-8; header=present', :filename => #filename)
This is in the controller generate_data and action csv_cars.
When I run this action (webste.com/generate_data/csv_cars), it will automatically pop up a window to download the file.
But how to write the CSV content to the action? So when I open the URL, I'll see there written the content from the database?
I know this is an old thread but I came across it in my search so in case someone else does the same, here's my answer and what worked for me.
I think bhanu had a good way of going about it but I did change something. Instead of doing #cars within the respond_to, I just called send_data Cars.to_csv since, as Rob stated, it was made as a class method. It worked beautifully for me.
class Car < ActiveRecord::Base
def self.to_csv(make)
attributes = %w{id name price} #customize columns here
cars = Car.where(maker_name: make)
CSV.generate(headers: true) do |csv|
csv << attributes
cars.each do |car|
csv << attributes.map{ |attr| car.send(attr) }
end
end
end
end
And then in the controller
class CarsController < ApplicationController
def index
send_data Cars.to_csv('Chevy'), filename: "cars-#{Date.today}.csv"
end
end
I understand that this will be called when you go to cars/index but you can put that into any method, if statement or anything you want and just have it called whenever you would like from there. You can also have arguments, as I did above with make, and query for certain fields. It was definitely a lot easier than I thought it was going to be. Hope this helped someone.
You need to do something like this.
def csv_cars
headers = ['column one', 'column two', 'column three']
csv_data = CSV.generate(headers: true) do |csv|
csv << headers
csv << ["A", "B", "C"]
end
send_data csv_data, filename: "data-#{Date.today.to_s}.csv", disposition: :attachment
end
define a to_csv method in your model as shown below
class Car < ActiveRecord::Base
def self.to_csv
attributes = %w{id name price} #customize columns here
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |car|
csv << attributes.map{ |attr| car.send(attr) }
end
end
end
end
Later in your controller
class CarsController < ApplicationController
def index
#cars = Car.all
respond_to do |format|
format.html
format.csv { send_data #cars.to_csv, filename: "cars-#{Date.today}.csv" }
end
end
end

rails csv generator not reaching the model

I have in my controller a section that generates an array of Song objects. For each song object, I want to output some of it's information to a csv file. I found instructions online and followed them to the best of my ability.
The controller:
def index
#stations = Station.all
#csv crap
#played = []
Station.first.users.each do |u|
u.playlists.where("updated_at > ?", Date.today).each do |p|
#played << p
end
end
#songs = []
if !#played.empty?
#played.each do |pl|
pl.songs.each do |s|
#songs << s
end
end
end
#data = []
#data << "Song"
#data << "Album"
#data << "Artist"
(0...#songs.length).each do |s|
son = #songs[s]
#data << [son.title, Album.find(son.album_id).name, Artist.find(son.artist_id).name]
end
respond_to do |format|
puts "******************\n\n\n\n\n\n\n\n"
puts #songs.inspect
puts "******************\n\n\n\n\n\n\n\n"
format.html
format.csv { send_data #data.to_csv(), filename:"daily_report.csv" }
end
end
The model:
def self.to_csv(options = {})
puts "fuck all"
CSV.generate(options) do |csv|
csv << column_names
options.each do |item|
csv << item.title
csv << item.attributes.values_at(*column_names)
csv << item.attributes.values_at(*column_names)
end
end
end
For some reason, the controller is never using the model to generate the csv, which it should be. I have to extra stuff in the controller to try to make it generate correctly there, but I really want it to generate in the model. Any idea why it never hits the self.to_csv in the model?
Your model's to_csv method isn't being called because you're not calling it - you're calling a to_csv instance method on an array.
You need something along the lines of
ModelClass.to_csv(#songs)
and your to_csv method should be closer to
def self.to_csv(data, options = {})
CSV.generate(options) do |csv|
csv << column_names
data.each do |item|
csv << [item.title]
csv << item.attributes.values_at(*column_names)
csv << item.attributes.values_at(*column_names)
end
end
end
I'm a bit confused about what you're adding to the csv (for example why are the item attributes added twice?) but this should at least produce a csv.

CSV export giving #<Product:0xba4618> and similar entries instead of real data?

I am following this http://railscasts.com/episodes/362-exporting-csv-and-excel and it looks to be pretty straight forward. I have included 'csv' and have put the code in controller index file and in model.
Now when I am clicking on /products.csv, it do export the list of products in csv but instead of having actual data it is having entries like #, what am I doing wrong ?
EDIT - here is the product.rb
attr_accessible :name, :size, :email
def self.to_csv
CSV.generate do |csv|
csv << column_names
all.each do |product|
csv << product.attributes.values_at(*column_names)
end
end
end
In your products controller define a method as below:
def export_csv
#Products = Product.find(:all)
csv = CSV.generate do |csv|
# header row
csv << ["product_name","product_type"]
# data row
#products.each do |t|
csv << [t.product_name, t.product_type]
end
end
send_data(csv, :type => 'text/csv')
end
In your routes:
match 'csv' => 'products#export_csv', :as => :csv
In your view give a link for CSV:
link_to "CSV", csv_path

Resources