2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rails Active Storage ファイルのパスを取得する方法

Last updated at Posted at 2020-07-29

投稿した理由

divタグのstyle属性の値background-imageを使う際にURLが必要になったから

やり方

rails_blob_pathを使う(has_one_attached :image)

show.html.erb
<div style="background-image: url(<%= rails_blob_path(@user.image) %>) "></div>

Active Storage インストール方法

①image magick(画像変換ツール)をインストール

$ sudo yum install -y ImageMagick

②image magickを使えるようにするために、mini_magick(gemファイル)をバンドルインストールする

③Active Storageのインストール

$ rails active_storage:install
$ rails db:migrate

おまけ

ActiveStorageメソッドあれこれ

  • image.attached? - 画像のアップロードがされているかを確かめるメソッド
2
5
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
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?