http://github.com/ilmotta/to-csv
Model:
def self.most_active(role)
select = "users.*"
find(:all, :conditions => ["role_id = ?", role], :limit => 13, :select => select, :order => 'login_count DESC')
end
Controller:
def export_users
#@users = User.all
#@users = User.find(:all)
role = params[:role]
@users = User.most_active(role)
respond_to do |format|
# format.csv { send_data(@users.to_csv) }
format.csv {
filename = "users-#{Time.now.strftime("%Y%m%d%H%M%S")}.csv"
send_data(@users.to_csv({:only => [:role_id,:last_login_ip,:login]}, {:col_sep => ','}), :type => "text/csv; charset=utf-8; header=present", :filename => filename)
}
end
end
url:
/export_users?role=10001
http://github.com/arydjmal/to_csv
http://github.com/wenke/to_csv
@posts = Post.all # # default are export header and all fileds # @posts.to_csv @posts.to_csv(:only => [:title, :body]) @posts.to_csv(:except => [:id]) @posts.to_csv(:header => false)
class PostsController < ApplicationController def index @posts = Post.all respond_to do |format| format.csv { send_data(@posts.to_csv) } #format.csv { # filename = "posts-#{Time.now.strftime("%Y%m%d%H%M%S")}.csv" # send_data(@posts.to_csv, :type => "text/csv; charset=utf-8; header=present", :filename => filename) #} end end end