Ruby on Rails - Export Records to CSV Files using Rails - ruby on rails tutorial - rails guides - rails tutorial - ruby rails



  • Ruby comes with an excellent CSV library that was formerly known as Faster CSV in Ruby 1.8.
  • We’ll use this library to generate our CSV data.
  • As it’s part of the standard library all we have to do is require it and we’ll do this in our application’s config file.
Ruby On Rails Csv
Learn Ruby On Rails Tutorials - Ruby On Rails Csv - Ruby On Rails Examples

Steps:

  • Add a controller and make sure you handle the csv request.
  • Add a route to point to your controller
  • Add a model with the class method to_csv
Export Records Csv File Rails
Learn Ruby On Rails Tutorials - Export Records Csv File Rails - Ruby On Rails Examples

Controller

  • The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services.
    • It is responsible for routing external requests to internal actions. It handles people-friendly URLs extremely well.
    • It manages caching, which can give applications orders-of-magnitude performance boosts.
    • It manages helper modules, which extend the capabilities of the view templates without bulking up their code.
    • It manages sessions, giving users the impression of an ongoing interaction with our applications.
# users_controller.rb
class UsersController < ApplicationController
  def index
    @users = User.all
    respond_to do |format|
      format.html
      format.csv { send_data @users.to_csv, filename: "users-#{Date.today}.csv" }
    end
  end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Routes

  • The routing module provides URL rewriting in native Ruby.
  • It's a way to redirect incoming requests to controllers and actions.
  • It replaces the mod_rewrite rules.
  • Best of all, Rails' Routing works with any web server. Routes are defined in app/config/routes.rb.
# routes.rb

...

resources :users, only: :index

...
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Model

# user.rb
class User < ActiveRecord::Base
  def self.to_csv
    attributes = %w{id email name}
    CSV.generate(headers: true) do |csv|
      csv << attributes
      all.each do |user|
        csv << attributes.map{ |attr| user.send(attr) }
      end
    end
  end

  def name
    "#{first_name} #{last_name}"
  end
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team

Core custom methods:

  • to_csv will and will map out the "id, email, name" values of your model collection and then generate a CSV string.

Rails core methods for this feature:

  • #send_data Sends the given binary data to the browser.
  • This method is similar to render :text => data, but also allows you to specify whether the browser should display the response as a file attachment (i.e. in a download dialog) or as inline data.

This ruby on rails tutorial page provides you the following key areas such as ruby , rail , ruby on rails , rail forum , ruby on rails tutorial , ruby tutorial , rails guides , rails tutorial , learn ruby , rails form_for , ruby rails , ruby class , what is ruby on rails , rails installer , ruby online , learn ruby on rails , ruby on rails jobs , rails find_by , install rails , easyrail , rubyonrails , link_to rails , ruby on rails developer , learn ruby the hard way , railscasts , ruby on rails examples , ruby on rails vs php , rails 4 , rails activerecord , rails generate , ruby and rails , ruby on rails download , install ruby on rails , ruby net http , what is rails , ruby app , ruby vs ruby on rails , ruby on rails windows , rails for zombies , ruby on rails book , ruby on rails development , ruby on rails ide , ruby on rails tutorial pdf

Related Searches to Export Records to CSV Files using Rails