Controlに下記のようなコードを記入したのですがlocalhost:3000/chatにアクセスしても NoMethodError in ChatController#index
undefined method `id' for nil:NilClass と出てしまいます。
他のページでは正常に表示されるのですがなぜかindexでは正常に表示されません。
確認したところちゃんとログインも出来た状態でした。
###chat_controller.rb
class ChatController < ApplicationController def create current_user_chat_rooms = ChatRoomUser.where(user_id: current_user.id).map(&:chat_room) chat_room = ChatRoomUser.where(chat_room: current_user_chat_rooms, user_id: params[:user_id]).map(&:chat_room).first if chat_room.blank? chat_room = ChatRoom.create ChatRoomUser.create(chat_room: chat_room, user_id: current_user.id) ChatRoomUser.create(chat_room: chat_room, user_id: params[:user_id]) end redirect_to action: :show, id: chat_room.id end def index @current_user = User.find_by(user_id: params[:user_id]) @chat_rooms = ChatRoom.joins(:chat_room_users). where("chat_room_users.user_id =?", @current_user.id) end def show chat_room = ChatRoom.find_by(id: params[:id]) @current_user = User.find_by(id: session[:user_id]) @chat_room_user = chat_room.chat_room_users. where.not(user_id: current_user.id).first.user @chat_room_myuser = chat_room.chat_room_users. where.not(user_id: current_user.id).last.user @chat_rooms = ChatRoom.joins(:chat_room_users). where("chat_room_users.user_id =?", @current_user.id) @chat_messages = ChatMessage.where(chat_room: chat_room).order(:created_at) end end
###chat_room.rb
class ChatRoom < ApplicationRecord has_many :chat_messages has_many :chat_room_users end
###chat_room_user.rb
class ChatRoomUser < ApplicationRecord belongs_to :chat_room belongs_to :user end
###index.html.erb
<% @chat_rooms.each do |e| %> <%= e.id %> <% end %>
###show.html.erb
<% provide(:title, @chat_room_user.username ) %> <div class="container center"> <div class="sidebar"> <%= link_to root_path(), method: :get do%> <div class="home-btn"> <%= image_tag 'right-nav/home', class: 'home-img' %> <span class="home-p">ホーム</span> </div> <% end %> <%= link_to home_path(), method: :get do%> <div class="global-btn"> <%= image_tag 'right-nav/global', class: 'global-img'%> <span class="global-p">グローバル</span> </div> <% end %> <% if current_user.try(:admin?) %> <%= link_to rails_admin_path(), method: :get do%> <div class="control-btn"> <%= image_tag 'right-nav/control.svg', class: 'control-img'%> <span class="control-p">管理画面</span> </div> <% end %> <% end %> <div class="group-btn"> <%= image_tag 'right-nav/group', class: 'group-img'%> <span class="group-p">グループ</span> </div> </div> <div class="main"> <% if @chat_room_myuser.id == @chat_room_user.id %> <header class="header"> <div class="chatPartner"> <img src="<%= @chat_room_user.image %>" class="dm-icon"> <div class="dm-username"><%= @chat_room_user.username %></div> <div class="dm-userid">@<%= @chat_room_user.user_id %></div> </div> </header> <div class="chat-history"> <%= render @chat_messages %> <% @chat_rooms.each do |e| %> <%= e.id %> <% end %> </div> <div class="text-box"> <form class="messageInputForm"> <div class="container"> <input type="text" data-behavior="room_speaker" class="messageInputForm_input" placeholder="メッセージを入力..."> </div> </form> </div> </div> </div> <% else %> <div class="error-message"> <p class="sorry-chat">Sorry</p> <p class="sorry-text">ページを表示する権限がありません</p> </div> <% end %>
回答1件
あなたの回答
tips
プレビュー