0
0

More than 1 year has passed since last update.

Rails 画像投稿機能(CarrierWave)を実装する

Last updated at Posted at 2022-08-02

始めに

現在ポートフォリオを作成中で、画像投稿機能を実装したくなった。どうやらCarrierWaveというgemがあるらしいので実装してみる。

CarrierWave とは

railsアプリに画像投稿機能を実装するためのgem

単語 意味
Carrier 運搬人、郵便配達員、新聞配達人、運送業者
Wave 波、波浪、波動、起伏、うねり

公式ページ

公式ページ(README)を見てみる

CarrierWave
This gem provides a simple and extremely flexible way to upload files from Ruby applications. It works well with Rack based web applications, such as Ruby on Rails.

このgemは提供します、Rubyアプリケーションからファイルをアップロードする単純で非常に柔軟な方法を。
このgemRuby on Railsのような*Rack basedなwebアプリケーションと相性がいいです。
*Rack basedなwebアプリケーションについての解説は割愛

インストール方法

Installation
Install the latest release
最新版をインストールしてください

 gem install carrierwave

In Rails, add it to your Gemfile

Railsの場合、Gemfileに追加してください

application/Gemfile
gem 'carrierwave', '~> 2.0'

Finally, restart the server to apply the changes.

最後に、変更を適用するためにサーバーを再起動する。

As of version 2.0, CarrierWave requires Rails 5.0 or higher and Ruby 2.2 or higher. If you're on Rails 4, you should use 1.x.

CarrierWave version 2.0以降ではRails5.0以降とRuby2.2以降が必要。もしあなたがRails4.xならCarrierWaveversion1.x.にすべき。

始め方

Getting Started
Start off by generating an uploader

アップローダーを作成するところから始まる。

ターミナル
rails generate uploader Avatar

this should give you a file in:

このコマンドでファイルが作成され、ここにファイルにあるはず

app/uploaders/avatar_uploader.rb

Check out this file for some hints on how you can customize your uploader. It should look something like this:
このファイルを確かめてみてくださいアップローダーのカスタマイズのためのヒントがあります。以下の様なファイルがあるはずです。

app/uploaders/avatar_uploader.rb
class AvatarUploader < CarrierWave::Uploader::Base
      #省略
  storage :file
      #省略
end

You can use your uploader class to store and retrieve files like this:

アップローダークラスを使って、以下のファイルのように保存したり取得したりすることができる

uploader = AvatarUploader.new

uploader.store!(my_file) #保存する!

uploader.retrieve_from_store!('my_file.png') #保存から取り出す!

CarrierWave gives you a store for permanent storage, and a cache for temporary storage. You can use different stores, including filesystem and cloud storage.
Most of the time you are going to want to use CarrierWave together with an ORM. It is quite simple to mount uploaders on columns in your model, so you can simply assign files and get going:

CarrierWaveはあなたに永続的な保存場所と、一時保存のためのキャッシュを与えてくれる。あなたは様々な保管場所を使うことができる。ファイルシステムやクラウドストレージなど。
たいていの場合、CarrierWaveをORMと一緒に使用することになるでしょう。アップローダーをモデル内のカラムにマウントするのは非常に簡単なので、簡単な手順でファイルを割り当てて実行することができます。

CarrierWaveのActiveRecordメソッド

Make sure you are loading CarrierWave after loading your ORM, otherwise you'll need to require the relevant extension manually, e.g.:

確認してください。ORMを読み込んだ後にCarrierWaveが読み込まれていることを。そうでない場合は手動で拡張機能を要求する必要がある。

require 'carrierwave/orm/activerecord'

Add a string column to the model you want to mount the uploader by creating a migration:

アップローダーを搭載したいモデルに文字列のカラムを追加する、マイグレーションファイルを作成して。

ターミナル
rails g migration add_avatar_to_users avatar:string
rails db:migrate

Open your model file and mount the uploader:

モデルファイルを開きアップローダーを搭載する

class User < ApplicationRecord
  mount_uploader :avatar, AvatarUploader
end

マイグレーションファイル
2022/08/02 執筆中

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0