・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina
The following has been implemented.
・ Slim introduction ・ Introduction of Bootstrap3 ・ Implementation of posting function
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
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
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
filename: 'Book list information'
slim:books/index.html.slim
/Postscript
= link_to 'CSV export', books_path(format: :csv), class: 'btn btn-success'
Recommended Posts