質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1250閲覧

Active Storageでバリデーションをかけたいのにundefined method `each'となってしまう

kawasaki4563

総合スコア32

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/03/31 06:01

Active Storageでユーザー画像を登録するときに、動画を投稿されるといったことが起きないようにしようと思ったので、バリデーションをモデルにか毛用と思ったのですが、以下のようなエラーになってしまいます

NoMethodError in Devise::RegistrationsController#create undefined method `each' for #<ActiveStorage::Attached::One:0x00007f5566495d50> Did you mean? detach

ユーザーの登録はdeviseを用いています。

#問題のソースコード
ユーザーに関するモデル

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_one_attached :image has_many :posts has_many :likes has_many :comments, dependent: :destroy has_many :movies def liked_by?(post_id) likes.where(post_id: post_id).exists? end def liked_by?(movie_id) likes.where(movie_id: movie_id).exists? end with_options presence: true do validates :nickname validates :mania_histry validates :enjoy_point validates :email validates :password, length: { minimum: 6 } end validate :image_presence, :image_size, :image_length def image_presence if image.attached? if !image.content_type.in?(%('image/jpeg image/png')) errors.add(:image, 'にはjpegまたはpngファイルを添付してください') end else errors.add(:image, 'ファイルを添付してください') end end def image_size image.each do |image| if image.blob.byte_size > 5.megabytes image.purge errors.add(:images, "は1つのファイル5MB以内にしてください") end end end def image_length if image.length > 1 image.purge errors.add(:image, "は1枚だけです") end end end

ユーザーに関するコントローラー

class ApplicationController < ActionController::Base before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :mania_histry, :enjoy_point, :image ]) end end

登録画面

<h2>新規登録 </h2> <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= render "devise/shared/error_messages", resource: resource %> <div class="field"> <%= f.label :email, 'メールアドレス' %><br /> <%= f.email_field :email, autofocus: true, autocomplete: "email" %> </div> <div class="field"> <%= f.label :password, 'パスワード' %> <% if @minimum_password_length %> <em>(<%= @minimum_password_length %> 字以上で入力)</em> <% end %><br /> <%= f.password_field :password, autocomplete: "new-password" %> </div> <div class="field"> <%= f.label :password_confirmation, 'パスワード(確認用)'%><br /> <%= f.password_field :password_confirmation, autocomplete: "new-password" %> </div> <div class="field"> <%= f.label :nickname,'ニックネーム' %> <%= f.text_field :nickname, autocomplete: "name" %> </div> <div class="field"> <%= f.label :mania_histry,'趣味歴' %> <%= f.number_field :mania_histry, autocomplete: "mania_histry" %> </div> <div class="field"> <%= f.label :enjoy_point, '楽しいところ' %> <%= f.text_field :enjoy_point, autocomplete: "enjoy_point" %> </div> <div class="field"> <%= f.label :image,'ユーザー画像' %> <%= f.file_field :image %> </div> <div class="actions"> <%= f.submit "登録する",class:'button' %> </div> <%= render "devise/shared/links" %> <% end %>

#やってみたこと
とりあえずエラー文に乗っていると売りにeachの部分をdetachに変えたのですがだめでした。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

def image_size image.each do |image| if image.blob.byte_size > 5.megabytes image.purge errors.add(:images, "は1つのファイル5MB以内にしてください") end end end
eachで回しているのはなぜですか? imageは一つだけなのですから
def image_size if image.blob.byte_size > 5.megabytes image.purge errors.add(:images, "は1つのファイル5MB以内にしてください") end end
ではだめ?

投稿2021/03/31 09:33

winterboum

総合スコア23329

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kawasaki4563

2021/04/01 00:08

なるほど確かに複数枚投稿だったらeach文にしたほうがいいけどそうじゃないんだったらeachである必要ってないですよね。 目からうろこでした
kawasaki4563

2021/04/01 00:09

いつもありがとうございます
winterboum

2021/04/01 00:17

必要ない のではなく してはだめ です。eadhが無いんですから
kawasaki4563

2021/04/01 00:25

そうですね。ありがとうございます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問