[Ruby on Rails] How to use CarrierWave

What is Carrier Wave?

A gem that allows you to easily add file uploads.

Carrier Wave installation

After adding the following to Gemfile, execute the command to install the gem.

gem 'carrierwave'
$ bundle install

Creating an uploader

Running the rails g uploader uploader name command creates the app/uploaders/image_uploader.rb file. This time, the uploader name is image.

$ rails g uploader image

Linking with the model

mount_uploader: Uploader name, class name will be added to the associated model.

class Post < ApplicationRecord
  mount_uploader :image, ImageUploader
end

Edit controller

Create a controller to upload the image. Create as usual without being aware of uploading images. Since there are text and image columns, the strong parameters are described as follows.

class PostsController < ApplicationController

 private
 def post_params
  params.permit(:image, :content)
 end

end

Edit view

Create a form to upload the image. Create as usual without being aware of uploading images. For example, it looks like this.

<%= form_with url: "/posts/create" do |f| %>
  <div class="form">
    <%= f.label "Posting a photo" %><br>
    <%= f.file_field :image %><br>
    <%= f.text_area :content, value: @post.content %><br>
    <%= f.submit "Post" %>
   </div>
<% end %>

When you upload an image using this form, the image will be saved in the public/uploads folder.

By adding <% if @ user.image?%>, You can check whether the image has been set and conditionally branch depending on whether the image is set or not.

Display the saved image

To display the saved image, call it with image_tag.

<%= image_tag @post.image_url %>

Adding if @ post.image? will prevent you from getting an error when there is no image.

<%= image_tag @post.image_url if @post.image? %>

Recommended Posts

[Ruby on Rails] How to use CarrierWave
How to use Ruby on Rails
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
[Ruby on Rails] How to use session method
[Rails] How to use enum
How to add / remove Ruby on Rails columns
How to use Ruby return
[Rails] How to use enum
How to use rails join
Ruby: How to use cookies
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] How to use Scope
[Ruby on Rails] How to write enum in Japanese
[Ruby on Rails] How to change the column name
[Ruby On Rails] How to reset DB in Heroku
(Ruby on Rails6) How to create models and tables
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
[Rails] How to use devise (Note)
[Rails] How to use flash messages
How to deploy Bootstrap on Rails
How to use Bio-Formats on Ubuntu 20.04
[Ruby] How to use any? Method
Rails on Tiles (how to write)
[Rails] How to use Active Storage
[Introduction to Rails] How to use render
How to use Ruby inject method
How to display a graph in Ruby on Rails (LazyHighChart)
A memorandum on how to use Eclipse
How to use Apache Derby on Eclipse
How to use custom helpers in rails
[Rails] How to upload images using Carrierwave
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
[Rails] How to use rails console with docker
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Rails] How to use the map method
Ruby length, size, count How to use
How to use MySQL in Rails tutorial
Deploy to Heroku [Ruby on Rails] Beginner
[Ruby] How to use slice for beginners
Preparing to introduce jQuery to Ruby on Rails
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to use video_tag to display videos
[Ruby on Rails] Button to return to top
Ruby: CSV :: How to use Table Note
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
[Ruby on Rails] Use the resources method to automatically create routes.
[Ruby on Rails] How to avoid creating unnecessary routes for devise
How to use Map
How to write Rails
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for