When creating the original application, we implemented a function to post images, so I will post it so that I will not forget it. There were various methods when I looked it up with Qiita etc., but this time I used a function called Active Storage of Rails.
Terminal
% brew install imagemagick
Gemfile
gem 'mini_magick'
gem 'image_processing', '~> 1.2'
Terminal
rails active_storage:install
Terminal
rails db:migrate
This time we will implement it assuming that it will be entwined with the Post table
Post model
class Post < ApplicationRecord
has_one_attached :image
end
Post controller
#Omission
private
def post_params
params.require(:post).permit(:hoge, :fuga, :image)
end
Display an image using the image_tag method
Post view
#Omission
<div class='post'>
<%= image_tag post.image, class: 'post-image' %>
</div>
Recommended Posts