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.
gemfile
gem '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.
Model name.rb
class model name< ActiveRecord::Base
mount_uploader :Column name you want to associate,Uploader class
end
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!
Recommended Posts