前提・実現したいこと
Rails初心者です。
現在Railsでアプリを作っており、Active Storageを使って画像をテーブルに保存しているのですが、ファイル選択を必ずさせるためのバリデーションがわからず質問させていただきました。
ご回答いただけると非常に助かります。
発生している問題・エラーメッセージ
画像ファイル選択を必須にさせる
該当のソースコード
models.user.rb
class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :email, presence: true validates :name, presence: true validates :profile, presence: true validates :avatar, presence: true has_one_attached :avatar has_many :tweets has_many :comments acts_as_followable # フォロワー機能 acts_as_follower # フォロー機能 has_many :messages, dependent: :destroy has_many :entries, dependent: :destroy end
試したこと
validates :avatar, presence: trueとしても空の記述ということではないので、バリデーションはできませんでした。
補足情報(FW/ツールのバージョンなど)
Ruby version 2.5.1
Rails version 5.2.3
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。