Q&A
実現したいこと
ユーザーページで、ログイン中のユーザー情報(プロフィール画像)を取得したい。
前提
Railsで本の紹介サイトを作成しています。
ユーザー一覧ページ(users_index)、ユーザー詳細ページ(users_show)を作成途中にエラー発生したものです。
下記画像は、ユーザー関係なく投稿一覧が見れるページです。(books_index)
投稿一覧ページ(books_index)では画像左側の「User info」列でログイン中のユーザーを表示させています。
↓
ユーザー一覧ページ(users_index)でログイン中のユーザーを、ユーザー詳細ページ(users_show)では選択したユーザーを取得したいです。
発生している問題・エラーメッセージ
左側のUser infoは共通レイアウトを使用しているのですが、そのhtmlファイル(_share.html)内→users_indexファイルに適用したい際にエラーが発生しています。
プロフィール画像を取得する際にエラーが発生しているようです。
htmlファイル内の@user.get_profile_image箇所または、controllerファイル内での記述が間違っていると思いましたが、修正方法に辿り着けませんでした。
該当のソースコード
app/views/books/_share.html.erb
1<!-- 共通レイアウトページ --> 2<div class="col-md-3"> 3 <h2>User info</h2> 4 <!-- get_profile_image => user.profileに登録の画像を取り出している --> 5 <%= image_tag @user.get_profile_image(100,100) %> 6 <table class="table"> 7 <tbody> 8 <tr> 9 <th>name</th> 10 <th><%= @user.name %></th> 11 </tr> 12 <tr> 13 <th>introduction</th> 14 <th></th> 15 </tr> 16 </tbody> 17 </table> 18 <div class="row"> 19 <%= link_to edit_user_path(current_user.id), class: 'btn btn-outline-secondary btn-block' do %> 20 <i class="fas fa-user-cog"></i> 21 <% end %> 22 </div> 23 24 <h2 class="mt-3">New book</h2> 25 <%= form_with model: Book.new do |f| %> 26 <div class="form-group"> 27 <div class="field"> 28 <br><%= f.label "Title" %><br> 29 <%= f.text_field :title, class: 'form-control book_title' %> 30 </div> 31 </div> 32 33 <div class="form-group"> 34 <div class="field"> 35 <br><%= f.label "Opinion" %><br> 36 <%= f.text_area :body, class: 'form-control book_body' %> 37 </div> 38 </div> 39 40 <div class="form-group"> 41 <%= f.submit "Create Book", class: 'btn btn-success' %> 42 </div> 43 44 <% end %> 45</div>
app/views/users/index.html.erb
1<!-- User indexページ--> 2<div class="container px-5 px-sm-0"> 3 <div class="row"> 4 <%= render 'books/share', books: @books, users: :@users %> 5 6 <div class="col-md-8 offset-md-1"> 7 <h2>Users</h2> 8 <table class="table"> 9 <thead> 10 <th>image</th> 11 <th>name</th> 12 <th colspan="3"></th> 13 </thead> 14 15 <tbody> 16 <% @users.each do |user| %> 17 <tr> 18 <td><%= image_tag @user.get_profile_image(100,100) %></td> 19 <td><%= user.name %></td> 20 <td><%= link_to "show", user_path(user.id) %></td> 21 </tr> 22 <% end %> 23 </tbody> 24 </table> 25 </div> 26 </div> 27</div>
app/models/user.rb
1 has_one_attached :profile_image 2 3 def get_profile_image(width, height) 4 unless profile_image.attached? 5 file_path = Rails.root.join('app/assets/images/no_image.jpg') 6 profile_image.attach(io: File.open(file_path), filename: 'default-image.jpg', content_type: 'image/jpeg') 7 end 8 profile_image.variant(resize_to_limit: [width, height]).processed 9 end
app/controllers/users_controller.rb
1class UsersController < ApplicationController 2 def show 3 @user = User.find(params[:id]) 4 @users = User.all 5 @book = Book.new 6 @books = @user.books 7 end 8 9 def edit 10 @user = User.find(params[:id]) 11 end 12 13 def index 14 @user = current_user.id 15 @users = User.all 16 @book = Book.new 17 @books = Book.all 18 end 19 20 private 21 22 def book_params 23 params.require(:book).permit(:image, :title, :body) 24 end 25 26end 27
migration
1class AddUserIdToBooks < ActiveRecord::Migration[6.1] 2 def change 3 add_column :books, :user_id, :integer 4 end 5end 6
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
2023/03/08 20:55
2023/03/10 14:01