LoginSignup
0
0

More than 3 years have passed since last update.

rails 発展その13 画像について

Posted at

railsの画像に関する内容です。

ImageMagick

ImageMagickは、コマンドラインから簡単に画像の保存形式の変更などが行えるツールです。

ターミナル

ターミナル
$ brew install imagemagick

ImageMagickがインストールできたら、mini_magickをインストールします。
gemfileに追記しましょう。

gemfile
gem 'mini_magick'

次にターミナルでbundle installします。

ターミナル
$ bundle install

Active Storage

rails active_storage:installコマンドを実行すると
Active Storageが使用するテーブル用のマイグレーションファイルが作成されます。

ターミナル
$ rails active_storage:install
$ bundle exec rake db:migrate

Active Storage用の設定として

model
   class User < ApplicationRecord
     has_one_attached :avatar
   end

この記述を追加することで、ユーザーのレコードと画像を紐づけることができます。ユーザーテーブルにカラムを追加する必要はありません。

image_tag

image_tagは、htmlのタグを生成するヘルパーメソッドです。

sample.html.erb
  <%= image_tag "image/sample.jpg" %> 
  <!-- <img src="image/sample.jpg">  -->
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