Advertisement
huyhung94

Export to CSV

Feb 13th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.89 KB | None | 0 0
  1. # Model
  2.     def self.to_csv(data, options = {})
  3.       desired_columns = ["id", "name", "phone", "email", "created_at"]
  4.       CSV.generate(options) do |csv|
  5.         csv << desired_columns
  6.         data.each do |u|
  7.           csv << u.attributes.values_at(*desired_columns)
  8.         end
  9.       end
  10.     end
  11.  
  12. # Controller
  13.     def index
  14.       params = filter_params
  15.  
  16.       users= UserServices.fetch_data(params)
  17.       @users = users.paginate(:page => params[:page], :per_page => 50)
  18.       respond_to do |format|
  19.         format.html
  20.         format.csv { send_data UserServices.to_csv(users) }
  21.       end
  22.     end
  23.  
  24. # View
  25.     %form{method: :GET}
  26.         .small-4.columns
  27.           .small-6.columns
  28.             %input.button.small{:type => "submit", :value => "Thực hiện"}
  29.           .small-6.columns
  30.             =link_to 'Export', user_index_path(format: "csv"), class: 'button small', id: 'user-export'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement