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

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

新規登録して質問してみよう
ただいま回答率
85.49%
Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

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

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

Q&A

解決済

1回答

2446閲覧

ActiveStorage プロフィール画像表示エラー

tm0308

総合スコア10

Cloud9

Cloud9は、クラウドからのプログラミングが可能になるWebサービス。IDEとしての機能が搭載されており、GitHubやHerokuなど他ツールとの連携も可能です。ブラウザ上で動くため、デバイスに関係なく開発環境を準備できます。

Ruby on Rails 6

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

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

AWS(Amazon Web Services)

Amazon Web Services (AWS)は、仮想空間を機軸とした、クラスター状のコンピュータ・ネットワーク・データベース・ストーレッジ・サポートツールをAWSというインフラから提供する商用サービスです。

0グッド

0クリップ

投稿2022/09/22 10:16

前提

Ruby on Rails6初学者です。
Webアプリケーションを作成しております。

ユーザー新規登録時にassets/imagesフォルダに格納してあるデフォルト画像を呼び出し表示するよう設定しておりますが、
もともと正常に登録できていたものがレイアウト等変更の後、
rails db:reset後にエラーが出るようになってしまいました。

ユーザー登録時には画像登録をせずデフォルト画像を表示させ
編集画面で任意で設定できるようにしております。

コンソールで確認を行ったところユーザー情報の登録はできておりました。

実現したいこと

  • ▲▲ユーザー詳細ページでプロフィール画像を表示させる

発生している問題・エラーメッセージ

ActiveStorage::FileNotFoundError in Public::Users#show profile_image.attach(io: File.open(file_path), filename: 'default-image.jpg', content_type: 'image/jpeg') end profile_image.variant(resize_to_limit: [width, height]).processed end

<ターミナル>

Started GET "/public/users/3" for 106.163.77.215 at 2022-09-22 09:14:59 +0000 Cannot render console from 106.163.77.215! Allowed networks: 127.0.0.0/127.255.255.255, ::1 Processing by Public::UsersController#show as HTML Parameters: {"id"=>"3"} User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] ↳ app/controllers/public/users_controller.rb:4:in `show' User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 3], ["LIMIT", 1]] ↳ app/controllers/public/users_controller.rb:6:in `show' Rendering layout layouts/application.html.erb Rendering public/users/show.html.erb within layouts/application ActiveStorage::Attachment Load (0.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 3], ["record_type", "User"], ["name", "profile_image"], ["LIMIT", 1]] ↳ app/models/user.rb:10:in `get_profile_image' TRANSACTION (0.1ms) begin transaction ↳ app/models/user.rb:12:in `get_profile_image' User Exists? (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."nick_name" = ? AND "users"."id" != ? LIMIT ? [["nick_name", "ゲスト"], ["id", 3], ["LIMIT", 1]] ↳ app/models/user.rb:12:in `get_profile_image' TRANSACTION (0.1ms) rollback transaction ↳ app/models/user.rb:12:in `get_profile_image' ActiveStorage::Blob Load (0.2ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" INNER JOIN "active_storage_attachments" ON "active_storage_blobs"."id" = "active_storage_attachments"."blob_id" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 3], ["record_type", "User"], ["name", "profile_image"], ["LIMIT", 1]] ↳ app/models/user.rb:14:in `get_profile_image' Disk Storage (0.4ms) Downloaded file from key: 5xi626jqqeor6lq6d02xnqh8g1b2 Rendered public/users/show.html.erb within layouts/application (Duration: 88.7ms | Allocations: 29007) Rendered layout layouts/application.html.erb (Duration: 89.5ms | Allocations: 29078) Completed 500 Internal Server Error in 100ms (ActiveRecord: 3.0ms | Allocations: 33118) ActionView::Template::Error (ActiveStorage::FileNotFoundError): 6: <div class="my-item"> 7: <% if @user.is_user == true %> 8: <div class="profile"> 9: <%= image_tag @user.get_profile_image(200, 200), class: "profile" %> 10: </div> 11: <div class="item"> 12: <%= @user.name %>(<%= @user.name_kana %>) app/models/user.rb:14:in `get_profile_image' app/views/public/users/show.html.erb:9

該当のソースコード

model/User

1class User < ApplicationRecord 2 3 devise :database_authenticatable, :registerable, 4 :recoverable, :rememberable, :validatable 5 6 has_one_attached :profile_image 7 8 def get_profile_image(width, height) 9 unless profile_image.attached? 10 file_path = Rails.root.join('app/assets/images/no_image.jpg') 11 profile_image.attach(io: File.open(file_path), filename: 'default-image.jpg', content_type: 'image/jpeg') 12 end 13 profile_image.variant(resize_to_limit: [width, height]).processed 14 end

users/show.html.erb

1<div class="user-page"> 2 <div class="container 1"> 3 <h1 class="mypage-title">User page</h1> 4 </div> 5 <div class="container 2"> 6 <div class="my-item"> 7 <% if @user.is_user == true %> 8 <div class="profile"> 9 <%= image_tag @user.get_profile_image(200, 200), class: "profile" %> 10 </div> 11 <div class="item"> 12 <%= @user.name %>(<%= @user.name_kana %>) 13 </div> 14 <div class="item"> 15 <%= @user.nick_name %> 16 </div> 17 <% elsif @user.is_user == false %> 18 <div class="profile"> 19 <%= image_tag @user.get_profile_image(200, 200), class: "profile" %> 20 </div> 21 <div class="item"> 22 <%= @user.name %>(<%= @user.name_kana %>) 23 </div>

試したこと

①下から2行目の.processedを抜いたところエラーは出なくなり詳細ページは開きますが
画像は表示されない状態になります。

model/User

1class User < ApplicationRecord 2 3 def get_profile_image(width, height) 456 profile_image.variant(resize_to_limit: [width, height]).processed 7 end

②model 対象箇所をデバッグしてみたところ

9: def get_profile_image(width, height) 10: binding.pry => 11: unless profile_image.attached? 12: file_path = Rails.root.join('app/assets/images/no_image.jpg') 13: profile_image.attach(io: File.open(file_path), filename: 'default-image.jpg', content_type: 'image/jpeg') 14: end 15: profile_image.variant(resize_to_limit: [width, height]).processed 16: end 2] pry(#<User>)> errors => #<ActiveModel::Errors:0x00007ffab410b768 @base= #<User id: 3, email: "guest@example.com", is_user: true, name: "ゲスト", name_kana: "ゲスト", nick_name: "ゲスト", brand_id: nil, store_id: nil, created_at: "2022-09-22 08:37:31.247198000 +0000", updated_at: "2022-09-22 08:37:31.247198000 +0000">, @errors=[]> [6] pry(#<User>)> profile_image => #<ActiveStorage::Attached::One:0x00007ffab42133e0 @name="profile_image", @record= #<User id: 3, email: "guest@example.com", is_user: true, name: "ゲスト", name_kana: "ゲスト", nick_name: "ゲスト", brand_id: nil, store_id: nil, created_at: "2022-09-22 08:37:31.247198000 +0000", updated_at: "2022-09-22 08:37:31.247198000 +0000">>

③また、正常の動作と比較を行ったところ、

<エラー時ターミナル>

Started GET "/public/users/3" for 106.163.77.215 at 2022-09-22 09:14:59 +0000 ・ ・ ActiveStorage::Attachment Load (0.1ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 3], ["record_type", "User"], ["name", "profile_image"], ["LIMIT", 1]] ↳ app/models/user.rb:10:in `get_profile_image' TRANSACTION (0.1ms) begin transaction ↳ app/models/user.rb:12:in `get_profile_image' User Exists? (0.1ms) SELECT 1 AS one FROM "users" WHERE "users"."nick_name" = ? AND "users"."id" != ? LIMIT ? [["nick_name", "ゲスト"], ["id", 3], ["LIMIT", 1]] ↳ app/models/user.rb:12:in `get_profile_image' TRANSACTION (0.1ms) rollback transaction ↳ app/models/user.rb:12:in `get_profile_image' ActiveStorage::Blob Load (0.2ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" INNER JOIN "active_storage_attachments" ON "active_storage_blobs"."id" = "active_storage_attachments"."blob_id" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 3], ["record_type", "User"], ["name", "profile_image"], ["LIMIT", 1]] ↳ app/models/user.rb:14:in `get_profile_image' Disk Storage (0.4ms) Downloaded file from key: 5xi626jqqeor6lq6d02xnqh8g1b2 Rendered public/users/show.html.erb within layouts/application (Duration: 88.7ms | Allocations: 29007) Rendered layout layouts/application.html.erb (Duration: 89.5ms | Allocations: 29078) Completed 500 Internal Server Error in 100ms (ActiveRecord: 3.0ms | Allocations: 33118)

<正常時ターミナル>

Started GET "/public/users/4" for 106.163.77.215 at 2022-09-22 07:06:39 +0000 ・ ・ ActiveStorage::Attachment Load (0.9ms) SELECT "active_storage_attachments".* FROM "active_storage_attachments" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 4], ["record_type", "User"], ["name", "profile_image"], ["LIMIT", 1]] ↳ app/models/user.rb:37:in `get_profile_image' TRANSACTION (0.1ms) begin transaction ↳ app/models/user.rb:39:in `get_profile_image' ActiveStorage::Blob Load (0.2ms) SELECT "active_storage_blobs".* FROM "active_storage_blobs" INNER JOIN "active_storage_attachments" ON "active_storage_blobs"."id" = "active_storage_attachments"."blob_id" WHERE "active_storage_attachments"."record_id" = ? AND "active_storage_attachments"."record_type" = ? AND "active_storage_attachments"."name" = ? LIMIT ? [["record_id", 4], ["record_type", "User"], ["name", "profile_image"], ["LIMIT", 1]] ↳ app/models/user.rb:39:in `get_profile_image' ActiveStorage::Blob Create (3.0ms) INSERT INTO "active_storage_blobs" ("key", "filename", "content_type", "metadata", "service_name", "byte_size", "checksum", "created_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["key", "ivn1e8fb65pg0cvqs3882n25oi4l"], ["filename", "default-image.jpg"], ["content_type", "image/jpeg"], ["metadata", "{\"identified\":true}"], ["service_name", "local"], ["byte_size", 2460], ["checksum", "bShwKl/QnL1aafFo0uJybg=="], ["created_at", "2022-09-22 07:06:39.182792"]] ↳ app/models/user.rb:39:in `get_profile_image' ・ ・

ActiveStorage::Attachment Load 後の挙動が違うようですが追求の仕方がわからず
質問させていただきました。

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

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

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

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

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

guest

回答1

0

自己解決

User passwordのバリデーションをdeviseデフォルト設定のものとmodelの記述で二重になってしまい、
modelの記述のせいでactivestorageファイ呼び出しの際にもパスワードを聞かれてしまう、ということでした。
(modelのパスワードを消しdeviseのデフォルト設定を使用)
(rails db:resetを使ってしまっていたためrails db:migrate:resetを使用)

詳しくまたまとめたいと思っています。

投稿2022/09/27 04:13

編集2022/09/27 04:14
tm0308

総合スコア10

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問