Exporting CSV and Excel File In ROR.
It’s very common now a days to export data from a Web application in CSV or Excel format and in this post i will show how to do that in a Rails application. Exporting to CSV:- Step:1 /config/application.rb require File . expand_path( '../boot' , __FILE__ ) require 'csv' require 'rails/all' Step:2 /app/controllers/products_controller.rb class ProductsController < ApplicationController def index @products = Product . order( :name ) respond_to do | format | format . html format . csv { render text : @products . to_csv } end end end Step:3 /app/models/product.rb class Product < ActiveRecord : :Base attr_accessible :name , :price , :released_on 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 end Ste...