LoginSignup
0
0

【Rails】バリアントの生成方法【ActiveStorage】

Posted at

以下のようにすると画像のバリアントを生成できます。

app/models/user.rb
class User < ApplicationRecord
  has_one_attached :avatar do |attachable|
    attachable.variant :large, resize_to_limit: [128, 128]
    attachable.variant :medium, resize_to_limit: [64, 64]
    attachable.variant :small, resize_to_limit: [32, 32]
  end
end

バリアントのURLを取得するには以下のようにします。

Rails.application.routes.url_helpers.rails_representation_url(avatar.variant(:large).processed, host: "localhost", port: "3000")
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