[rails] How to post images

Image posting method

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

1. Add refile to gemfile

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

2. Add image_id column

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

3. Add attachment method

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

4. Add image_id to Strong Parameters

class UserController < ApplicationController
  #abridgement

  private
  def list_params
    params.require(:user).permit(:name, :email, :image)
 end
end

5. Embed f.attachment_field in view file

Next, write as follows on the page to post the image.

<%= f.attachment_field :image %>

Recommended Posts

[rails] How to post images
How to write Rails
[Rails] How to upload images using Carrierwave
How to uninstall Rails
[Rails] How to upload multiple images using Carrierwave
How to post images on Heroku + CarrierWave + S3
How to handle uploaded images
[Rails] How to use enum
[Rails] How to install devise
How to minimize Java images
[Rails] How to use enum
How to read rails routes
How to use rails join
How to terminate rails server
How to write Rails validation
How to write Rails seed
[Rails] How to use validation
[Rails] How to disable turbolinks
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to implement scraping
[Rails] How to make seed
How to write Rails routing
[Rails] How to install simple_calendar
[Rails] How to install reCAPTCHA
[Rails] How to use Scope
How to download images from AWS S3 (rails, carrierwave)
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to install Font Awesome
[Rails] How to use devise (Note)
[Rails] How to use flash messages
[rails] How to display db information
[Rails] How to write in Japanese
[Rails] How to prevent screen transition
How to use Java HttpClient (Post)
How to use Ruby on Rails
How to deploy Bootstrap on Rails
[Rails] How to speed up docker-compose
[Rails] How to add new pages
Rails on Tiles (how to write)
[Rails] How to write exception handling?
[Rails] How to install ImageMagick (RMajick)
[Rails] How to install Font Awesome
[Rails] How to use Active Storage
How to introduce jQuery in Rails 6
[Rails] How to implement star rating
How to return Rails API mode to Rails
How to get along with Rails
[Introduction to Rails] How to use render
How to install Swiper in Rails
[Rails] How to delete images uploaded by carrierwave (using devise)
How to implement search functionality in Rails
How to change app name in rails
How to use custom helpers in rails
[Ruby on Rails] How to use CarrierWave
[Rails] How to convert from erb to haml
[Rails] How to use rails console with docker
How to insert a video in Rails
How to switch thumbnail images with JavaScript
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)