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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

Q&A

解決済

1回答

554閲覧

Railsで画像表示でエラーがでてしまいます

uruman

総合スコア12

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/10/20 08:03

前提・実現したいこと

Rilasで画像を埋め込もうとしたところ、エラーが発生しました。
また、同じページないでユーザプロフィール画像の表示には成功しています。

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

NoMethodError in Users#show undefined method `profile_image' for #<Book:0x00007fd3d94f9700> <% @user.books.each do |user| %> <tr> <td><%=attachment_image_tag(user, :profile_image, :fill, 63, 63, fallback: "no_image-icon.jpg")%></td> <td><%= @user.name %></td> <td><%=link_to "Show",user_path(user), class: "user_#{user.id}" %></td> </tr>

該当のソースコード

html

1<p id = "notice"></p> 2 <div class = 'container px-5 px-sm-0'> 3 <div class = 'row'> 4 <div class = 'col-md-3'> 5 <h2>User info</h2> 6 <%= render 'users/profile',user: @user %> 7 8 9 <h2 class = "mt-3">New book</h2> 10 <%= render 'books/form',book: @book %> 11 </div> 12 13 14 <div class = 'col-md-8 offset-md-1'> 15 <h2>Users</h2> 16 <table class = 'table'> 17 <thead> 18 <tr> 19 <th>image</th> 20 <th>name</th> 21 <th colspan="3"</th> 22 </tr> 23 </thead> 24 <tbody> 25 <% @user.books.each do |user| %> 26 <tr> 27 <td><%=attachment_image_tag(user, :profile_image, :fill, 63, 63, fallback: "no_image-icon.jpg")%></td> 28 <td><%= @user.name %></td> 29 <td><%=link_to "Show",user_path(user), class: "user_#{user.id}" %></td> 30 </tr> 31 <%end%> 32 </tbody> 33 </table> 34 </div> 35 </div> 36 </div> 37
class DeviseCreateUsers < ActiveRecord::Migration[5.2] def change create_table :users do |t| ## Database authenticatable t.string :email, null: false, default: "" t.string :encrypted_password, null: false, default: "" ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at ## Rememberable t.datetime :remember_created_at ## Trackable # t.integer :sign_in_count, default: 0, null: false # t.datetime :current_sign_in_at # t.datetime :last_sign_in_at # t.string :current_sign_in_ip # t.string :last_sign_in_ip ## Confirmable # t.string :confirmation_token # t.datetime :confirmed_at # t.datetime :confirmation_sent_at # t.string :unconfirmed_email # Only if using reconfirmable ## Lockable # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at t.string :name t.text :introduction t.string :profile_image_id t.timestamps null: false end add_index :users, :email, unique: true add_index :users, :reset_password_token, unique: true # add_index :users, :confirmation_token, unique: true # add_index :users, :unlock_token, unique: true end end
class RenamePrifileImageIdColumnToUsers < ActiveRecord::Migration[5.2] def change rename_column :users, :prifile_image_id, :profile_image_id end end

model

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_many :books,dependent: :destroy 8 attachment :profile_image 9end

試したこと

migrateファイルのprofile_image_idがprifile_image_idになっていたので変更したところ、ユーザプロフィール画像は表示できるようになったのですが、他の箇所では、画像が表示できません。

補足情報(FW/ツールのバージョンなど)

Rails5.2.6

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

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

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

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

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

guest

回答1

0

自己解決

<td><%=attachment_image_tag(@book.user, :profile_image, :fill, 63, 63, fallback: "no_image-icon.jpg")%></td>に直したら解決しました

投稿2021/10/20 14:05

uruman

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問