Reputation: 65
I'm using the CSV class to do result exporting to csv. I have it working now, but it only exports my search query to CSV, not the results (which are displayed on my 'show; page).
search.rb
class Search < ActiveRecord::Base
def to_xls(options = {})
{
"Id" => id.to_s,
"firstname" => firstname,
"surname" => surname,
"officenumber" => officenumber,
"department" => department,
"division" => division,
"address" => address,
"notes" => notes
}
end
end
searches_controller.rb
def show
@search = Search.find(params[:id])
respond_to do |format|
format.html
format.xls { send_data @searches.to_xls, content_type: 'application/vnd.ms-excel', filename: 'search.xls' }
end
end
mime_types.rb
Mime::Type.register "application/vnd.ms-excel", :xls
Upvotes: 0
Views: 1230
Reputation: 848
You Should try this one,
def self.to_xls(options = {})
CSV.generate(options) do |csv|
csv << column_names
all.each do |product|
csv << product.attributes.values_at(*column_names)
end
end
end
Link should be like <%= link_to "Excel", your_path(format: "xls") %>
You can find complete flow here to implement it.
Upvotes: 2