[Rails] Implementation of CSV export function

Target

ezgif.com-video-to-gif.gif

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introductionIntroduction of Bootstrap3Implementation of posting function

Implementation

1. Edit ʻapplication.rb`

application.rb


require_relative 'boot'

require 'rails/all'
require 'csv' #Postscript

Bundler.require(*Rails.groups)

module Bookers2Debug
  class Application < Rails::Application
    config.load_defaults 5.2
  end
end

2. Edit the controller

books_controller.rb


def index
  @book = Book.new
  @books = Book.all
  #Postscript
  respond_to do |format|
    format.html
    format.csv do |csv|
      send_users_csv(@books)
    end
  end
end

#Postscript
def send_users_csv(books)
  csv_data = CSV.generate do |csv|
    header = %w(ID Registration date Posted by Title)
    csv << header
    books.each do |book|
      values = [book.id, book.created_at, book.user.name, book.title]
      csv << values
    end
  end
  send_data(csv_data, filename: 'Book list information')
end

(1) Set the header of the CSV file.

header = %w(ID Registration date Posted by Title)
csv << header

(2) Set the contents of the CSV file.

books.each do |book|
  values = [book.id, book.created_at, book.user.name, book.title]
  csv << values
end

③ Set the CSV file name.

filename: 'Book list information'

3. Edit the view.

slim:books/index.html.slim


/Postscript
= link_to 'CSV export', books_path(format: :csv), class: 'btn btn-success'

Recommended Posts

[Rails] Implementation of CSV export function
[Rails 6] Implementation of search function
[Rails] Implementation of category function
[Rails] Implementation of tutorial function
[Rails] Implementation of like function
[Rails] Asynchronous implementation of like function
[Rails] Implementation of image preview function
[Rails] About implementation of like function
Rails [For beginners] Implementation of comment function
[Rails 6] Implementation of SNS (Twitter) sharing function
[Vue.js] Implementation of menu function Implementation version rails6
[Ruby on rails] Implementation of like function
[Vue.js] Implementation of menu function Vue.js introduction rails6
Implementation of search function
Rails search function implementation
Implementation of pagination function
[Rails] Implementation of search function using gem's ransack
Implementation of Ruby on Rails login function (Session)
[Rails 6] Implementation of inquiry function using Action Mailer
[Rails] Implementation of image enlargement function using lightbox2
[Rails] Implementation of retweet function in SNS application
Rails implementation of ajax removal
Rails fuzzy search function implementation
Implementation of sequential search function
Implementation of like function (Ajax)
Implementation of image preview function
Implementation of category pull-down function
Login function implementation with rails
[Rails 6] Pagination function implementation (kaminari)
Ruby on Rails <2021> Implementation of simple login function (form_with)
[Rails] Implementation of drag and drop function (with effect)
Implementation of Ruby on Rails login function (devise edition)
[Ruby on Rails] Implementation of tagging function/tag filtering function
[Rails] Implementation of multi-layer category function using ancestry "Preparation"
[Rails] Implementation of multi-layer category function using ancestry "seed"
[Rails] Implementation of SNS authentication (Twitter, Facebook, Google) function
[Rails] Implementation of user logic deletion
[Rails] Implementation of multi-layer category function using ancestry "Creation form"
Kaminari --Added pagination function of Rails
[Ruby on Rails] CSV output function
Rails sorting function implementation (displayed in order of number of like)
[Rails] Implementation of tagging function using intermediate table (without Gem)
[Rails] Implementation of many-to-many category functions
[Rails] gem ancestry category function implementation
[Ruby on Rails] Comment function implementation
[Rails 6] Like function (synchronous → asynchronous) implementation
Implement CSV download function in Rails
[Rails] Comment function implementation procedure memo
Implementation of like function in Java
[Rails 6] Implementation of new registration function by SNS authentication (Facebook, Google)
[Rails] Implementation of coupon function (with automatic deletion function using batch processing)
[Rails] Implementation of tag function using acts-as-taggable-on and tag input completion function using tag-it
[Rails] Addition of Ruby On Rails comment function
Implementation of user authentication function using devise (2)
Rails Addition of easy and easy login function
[Ruby on Rails] Follow function implementation: Bidirectional
Implementation of user authentication function using devise (1)
Rails Basic CRUD function implementation procedure scaffold
Implementation of user authentication function using devise (3)
[Rails] Implementation of validation that maintains uniqueness
Rails CSV basics