The procedure for posting an image is like this.
-Add refile to gemfile -Added image_id column -Add attachment method -Add image_id to Strong Parameters -Embed f.attachment_field in view file
Role of refile -Images can be easily incorporated. -Thumbnails can be generated. -You can set the file upload destination.
refile-mini-magick is a gem for resizing images.
#Image posting gem
gem "refile", require: "refile/rails", github: 'manfe/refile'
#Image processing (size adjustment, etc.) gem
gem "refile-mini_magick"
Don't forget to bundle install.
$ bundle install
Add an image_id column to the User table.
$ rails g migration AddImageIdToUsers image_id:string
Don't forget this too. Reflected in the database with $ rails db: migrate.
$ rails db:migrate
To use Refile, you need to add an attachment method to your model. The attachment method is required for the refile to access the specified column. This makes it possible to acquire and upload images that exist in the DB. The column name is image_id, but _id is not needed here.
app/models/user.rb
class User < ApplicationRecord
attachment :image
end
class UserController < ApplicationController
#abridgement
private
def list_params
params.require(:user).permit(:name, :email, :image)
 end
end
Next, write as follows on the page to post the image.
<%= f.attachment_field :image %>
Recommended Posts