0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

gemを使う?自分で書く?Starを確認しよう

Posted at

今回はgem導入時に自分が注意すべきだと思う点について話したいと思います。

それはズバリ

Starの数

です

gemって何?

gemとは、RailsやRubyのアプリケーションに追加の機能を提供するために使用されます。例えば、認証システム、ファイルアップロード、テストフレームワーク、データベース接続などの機能を提供するものがあります。

具体例

例えば、画像についてバリデーション処理を行いたい場合、
active_strage_validationを利用してもいいですが、このgemのstarを見ると、
image.png
1kしかstarがついていないことがわかります。

なので、このgemを入れるよりは、

class Article < ApplicationRecord
  has_one_attached :image

  validate :validate_image

  private

  def validate_image
    if image.attached?
      if !image.content_type.in?(%w[image/jpeg image/png])
        errors.add(:image, 'はJPEGまたはPNG形式でアップロードしてください。')
      elsif image.byte_size > 2.megabytes
        errors.add(:image, 'のサイズは2MB以下でなければなりません。')
      end
    else
      errors.add(:image, 'をアップロードしてください。')
    end
  end
end

のような感じでコード書いてあげたほうがうまくいきます。

備忘録でした

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?