Pdf is one of the popular formats today, with the advantage of being high quality and not prone to changing the font or layout of the content. So pdf is often used to store the content after it is completed and use them to exchange online. In some websites, many tasks also need to be stored in pdf format to facilitate the storage of users, so today I wrote this article to create a simple website using Ruby on Rails. PDF export function from web page information with PDFKit gem !!!
Create the Rails application
Create an application called invoices (invoices) and add 2 models:
1 2 3 4 5 6 | $ rails new invoices -d mysql $ cd invoices $ rails generate model invoice date:date client notes total $ rails generate model line_item description price:float invoice:references |
Then add some sample data to the database by adding the file db/seeds.rb.
1 2 3 4 5 6 7 8 9 | invoice = Invoice.create( client: 'Tùng Phan', total: 165000, date: Date.new(2020, 2, 17)) LineItem.create([ { invoice_id: invoice.id, description: 'Tuổi trẻ đáng giá bao nhiêu???', price: 96000 }, { invoice_id: invoice.id, description: 'Mắt Biếc (Tái Bản 2019)', price: 69000} ]) |
Run the rails db: seed command in the terminal to add the sample invoice to the database.
Create controller and view
run rails generate controller Invoices index show
to create controllers and views.
app / models / invoice.rb
1 2 3 4 | class Invoice < ApplicationRecord has_many :line_items end |
app / models / line_item.rb
1 2 3 4 | class LineItem < ApplicationRecord belongs_to :invoice end |
app / controllers / invoicescontroller.rb
1 2 3 4 | class InvoicesController < ApplicationController def index @invoices = Invoice.all end |
def show
@invoice = Invoice.find(params
1 2 |