前提
DM一覧を表示させたく、その際にユーザーの名前を表示させたいのですが、
らーが発生しました。
実現したいこと
- userのnameを表示させるために@anotherEntriesから抜き出したいのですが、
@anotherEntriesから相手userの名前を抜き出して表示させることは可能でしょうか?
発生している問題・エラーメッセージ
NoMethodError at /rooms undefined method `id' for #<ActiveRecord::Relation [#<Entry id: 21, user_id: 2, room_id: 24>]> Did you mean? ids
該当のソースコード
_room.html.erb
<ul class="list-unstyled"> <% @anotherEntries.each do |entry| %> <li class="mb-3 d-flex"> <div> <div> <p><%= link_to @anotherEntries.id.name, user_path(user), class: "text-decoration-none" %></p> </div> <div> <p><%= link_to @room.room_name, room_path(room), class: "text-decoration-none" %></p> </div> </div> </li> <% end %> </ul> <%== pagy_bootstrap_nav(@pagy) %>
試したこと
@anotherEntriesに相手user_idが入っているのでそこから名前を引き出すようにしたく、
@anotherEntries.user_id.nameで引き出そうとしたのですが不可能でした。
またDMの一覧表示させるのに@anotherEntriesで回すのは適切でしょうか?
補足情報
rooms_controller.rb
class RoomsController < ApplicationController before_action :authenticate_user! def index @currentEntries = current_user.entries myRoomIds = [] @currentEntries.each do |entry| myRoomIds << entry.room.id end @anotherEntries = Entry.where(room_id: myRoomIds).where('user_id != ?',current_user.id) end def create @plan = Plan.find(params[:entry][:plan_id]) @room = Room.create(room_name: @plan.title) @entry1 = Entry.create(room_id: @room.id, user_id: current_user.id) @entry2 = Entry.create(params.require(:entry).permit(:user_id, :room_id).merge(room_id: @room.id)) redirect_to "/rooms/#{@room.id}" end def show @room = Room.find(params[:id]) if Entry.where(user_id: current_user.id,room_id: @room.id).present? @messages = @room.messages @message = Message.new @entries = @room.entries else redirect_back(fallback_location: root_path) end end private def set_plan @plan = Plan.find(params[:id]) end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2022/10/05 09:36 編集
2022/10/05 11:52
退会済みユーザー
2022/10/05 13:34