前提・実現したいこと
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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。