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

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

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

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails 6

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

Q&A

解決済

1回答

1857閲覧

【Ruby On Rails】ユーザーページで、ユーザー情報(プロフィール画像)が取得できません。

sato_sato

総合スコア8

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby on Rails 6

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

0グッド

0クリップ

投稿2023/03/07 22:58

実現したいこと

ユーザーページで、ログイン中のユーザー情報(プロフィール画像)を取得したい。

前提

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

エラーに for 1:Integer って出てますね
@user がただの数字だからじゃないでしょうか

rb

1@user = current_user.id

userのインスタンスならこうですね

rb

1@user = current_user

投稿2023/03/08 01:50

yuma.inaura

総合スコア1453

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

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

sato_sato

2023/03/08 20:55

contoller.rbを修正したら無事解決しました! @userで画像を呼び出そうとしていたのに、idを入れてました、、、 ありがとうございます!
sato_sato

2023/03/10 14:01

以前ご回答頂きありがとうございました。 前回、エラーの解消はできたのですが、current_userを設定したことにより、他のユーザーの画像もログイン中のユーザー画像になり、投稿ユーザーごとの画像を表示させたいです。。 @user = User.find(params[:id]) に変えてみましたが、Couldn't find User without an IDとエラーメッセージが出ました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問