[RUBY] Add pdf file posting function with carrierwave

Introduction

As I mentioned in the article I wrote last time, I added a pdf posting function to the Rails application using carrierwave, so I will leave a memorandum of the procedure I took at that time.

Introduction of carrier wave

Add the line below to the gemfile and bundle install.

gemfile


gem 'carrierwave'

Add file column to db

This time, in order to save the sent data in db, I added a file column to the posts table that I originally created. I just created a migration file and added columns, so I will omit the command.

Creating a carrierwave class

To use carrierwave, you need to create a dedicated class. I referred to the following article for an overview of how to use it. https://pikawaka.com/rails/carrierwave

The following is a summary of the steps I took, although they are almost the same as the above page.

Generate carrierwave class with bin/rails g command

terminal


bundle exec rails g uploader uploader name

Describe as follows in the configuration file created in the following location app/uploaders/uploader name_uploader.rb.

app/uploaders/Uploader name_uploader.rb


class uploader class< CarrierWave::Uploader::Base
  #Setting the storage location
  storage :file

  #Setting the folder to save
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
end

This time, save the posted pdf file in the uploads folder under public. At the time of deployment, it seems that it can also be specified to an external storage service.

Linking uploader class and model

In the model you want to associate with the class, write the following.

Model name.rb


class model name< ActiveRecord::Base
  mount_uploader :Column name you want to associate,Uploader class
end

Add file_field to form & save to db

The information of file_field is saved in the file column as a parameter. (The save location can be any column you want to save.) I will omit it because it is just described in the controller as usual.

Display in view

Write the following in view.

app/views/Any view


<object data="<%= @post.file.url %>" type="application/pdf" width="200" height="300"></object>

@post is an instance variable of the post model defined in the action that renders this view and has a column called file. The url to the file is specified there using the url method of the uploader class generated by carrierwave. (Please replace it with the model and column name you set.) You can specify the size of the thumbnail to be displayed with width and height.

I was able to display it!

At the end

There seems to be other methods of thumbnail generation other than those that use object, so if you are interested, you may want to check it out. If I thought that the thumbnail could not be implemented easily and it was a carrierwave error, it was a mistake in specifying form_with, so if you are stuck, you may want to check that as well.

Recommended Posts

Add pdf file posting function with carrierwave
[Rails] Voice posting function ~ Cloudinary, CarrierWave
Image posting function
I tried OCR processing a PDF file with Java
With Kotorin ―― 7. Scoping Function
Serverless Function with Micronaut
I tried OCR processing a PDF file with Java part2