質問編集履歴
7
解決したコード及びER図を追加。
title
CHANGED
File without changes
|
body
CHANGED
@@ -127,4 +127,54 @@
|
|
127
127
|
|
128
128
|
ruby 2.6.4p104
|
129
129
|
RubyGems 3.0.3
|
130
|
-
Rails 5.2.3
|
130
|
+
Rails 5.2.3
|
131
|
+
|
132
|
+
### 【追記】ER図
|
133
|
+

|
134
|
+
|
135
|
+
### 【追記】解決したコード
|
136
|
+
|
137
|
+
### rooms_controller.rb
|
138
|
+
|
139
|
+
```Ruby
|
140
|
+
def index
|
141
|
+
#以下、メッセージ機能に関する記述。変更なし。
|
142
|
+
@rooms = Room.page(params[:page]).per(3)
|
143
|
+
@user = @current_user
|
144
|
+
@currentEntries = @current_user.entries
|
145
|
+
myRoomIds = []
|
146
|
+
@currentEntries.each do | entry |
|
147
|
+
myRoomIds << entry.room_id
|
148
|
+
end
|
149
|
+
@anotherEntries = Entry.includes(:user, :room).where(room_id: myRoomIds).where('user_id != ?', @user.id).order(created_at: :desc)
|
150
|
+
|
151
|
+
# 既読機能についてはコントローラでは特に記述せず。ビューにて記述。
|
152
|
+
end
|
153
|
+
|
154
|
+
def show
|
155
|
+
#以下、メッセージ機能に関する記述。変更なし。
|
156
|
+
@rooms = Room.page(params[:page]).per(3)
|
157
|
+
@user = @current_user
|
158
|
+
@currentEntries = @current_user.entries
|
159
|
+
myRoomIds = []
|
160
|
+
@currentEntries.each do | entry |
|
161
|
+
myRoomIds << entry.room_id
|
162
|
+
end
|
163
|
+
@anotherEntries = Entry.includes(:user, :room).where(room_id: myRoomIds).where('user_id != ?', @user.id).order(created_at: :desc)
|
164
|
+
|
165
|
+
# ここから既読機能。ログイン中のユーザーが抱える「message」に関する「相手からの通知」のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
|
166
|
+
@current_user.passive_notifications.where(action: 'message', read: false, room_id: params[:id]).each do |notification|
|
167
|
+
notification.update_attributes(read: true)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
```
|
171
|
+
|
172
|
+
### rooms_index.html.erb と rooms_show.html.erb(既読機能に関する内容は同じ)
|
173
|
+
|
174
|
+
```Rails
|
175
|
+
<!-- ログイン中のユーザーが保持している各Entryの中で、未読メッセージがある場合countを繰り返し表示。-->
|
176
|
+
<% message_count = Notification.where(action: "message", visited_id: @current_user.id, visitor_id: e.user.id, room_id: e.room.id, read: false).count %>
|
177
|
+
<% if message_count > 0 %>
|
178
|
+
<div class="message-count date-and-count"><%= message_count %></div>
|
179
|
+
<% end %>
|
180
|
+
```
|
6
rooms_controller.rb内のindexに追記。
title
CHANGED
File without changes
|
body
CHANGED
@@ -80,9 +80,13 @@
|
|
80
80
|
myRoomIds << entry.room_id
|
81
81
|
end
|
82
82
|
@anotherEntries = Entry.includes(:user, :room).where(room_id: myRoomIds).where('user_id != ?', @user.id).order(created_at: :desc)
|
83
|
+
|
84
|
+
@notifications = @current_user.passive_notifications.where(action: 'message').includes([:visitor], [:visited]).order(created_at: :desc).page(params[:page]).per(7)
|
85
|
+
@notifications.where(checked: false).each do |notification|
|
86
|
+
notification.update_attributes(checked: true)
|
87
|
+
end
|
83
88
|
@unread = @notifications.where(room_id: params[:id], read: false).count
|
84
89
|
end
|
85
|
-
|
86
90
|
```
|
87
91
|
|
88
92
|
### rooms_index.html.erb
|
5
現状のコードに変更。エラー文は出なくなったので、エラー文は削除。
title
CHANGED
File without changes
|
body
CHANGED
@@ -66,12 +66,20 @@
|
|
66
66
|
# ログイン中のユーザーが抱える「message」に関する相手からの通知を取得。
|
67
67
|
@notifications = @current_user.passive_notifications.where(action: 'message').includes([:visitor], [:visited]).order(created_at: :desc).page(params[:page]).per(7)
|
68
68
|
# ログイン中のユーザーが抱える相手からの通知のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
|
69
|
-
@current_user.passive_notifications.find_by(
|
69
|
+
@current_user.passive_notifications.find_by(params[:id]).update(read: true)
|
70
70
|
# 該当のroom_id内にある未読メッセージの数を取得。
|
71
71
|
@unread = @notifications.where(room_id: params[:id], read: false).count
|
72
72
|
end
|
73
73
|
|
74
74
|
def index
|
75
|
+
@rooms = Room.page(params[:page]).per(3)
|
76
|
+
@user = @current_user
|
77
|
+
@currentEntries = @current_user.entries
|
78
|
+
myRoomIds = []
|
79
|
+
@currentEntries.each do | entry |
|
80
|
+
myRoomIds << entry.room_id
|
81
|
+
end
|
82
|
+
@anotherEntries = Entry.includes(:user, :room).where(room_id: myRoomIds).where('user_id != ?', @user.id).order(created_at: :desc)
|
75
83
|
@unread = @notifications.where(room_id: params[:id], read: false).count
|
76
84
|
end
|
77
85
|
|
@@ -80,95 +88,39 @@
|
|
80
88
|
### rooms_index.html.erb
|
81
89
|
|
82
90
|
```Rails
|
91
|
+
<div class="message-items">
|
92
|
+
<% @anotherEntries.each do |e| %>
|
93
|
+
<div class="message-item">
|
94
|
+
<div class="message-left">
|
95
|
+
<%= link_to room_path(e.room.id) do %>
|
96
|
+
<img src="<%= "/user_images/#{e.user.image_name}" %>">
|
97
|
+
<% end %>
|
98
|
+
</div>
|
99
|
+
<div class="message-center">
|
100
|
+
<div class="message-center-username">
|
101
|
+
<%= link_to room_path(e.room.id) do %
|
102
|
+
<%= e.user.name %>
|
83
|
-
|
103
|
+
<%= @unread %>
|
104
|
+
<% end %>
|
105
|
+
</div>
|
106
|
+
<div class="message-center-message">
|
107
|
+
<%= link_to room_path(e.room.id) do %>
|
108
|
+
<% dm = Message.find_by(id: e.room.message_ids.last).try(:content) %>
|
109
|
+
<%= truncate(dm, length: 9) %>
|
110
|
+
<% end %>
|
111
|
+
</div>
|
112
|
+
</div>
|
113
|
+
<div class="message-right">
|
114
|
+
<div class="message-date" style="color: #C0C0C0;"><%= e.updated_at.strftime("%Y/%m/%d") %></div>
|
115
|
+
<div class="message-time" style="color: #C0C0C0;"><%= e.updated_at.strftime("%H:%M") %></div>
|
116
|
+
</div>
|
117
|
+
</div>
|
118
|
+
<% end %>
|
119
|
+
</div>
|
84
120
|
```
|
85
121
|
|
86
122
|
### 補足情報(FW/ツールのバージョンなど)
|
87
123
|
|
88
124
|
ruby 2.6.4p104
|
89
125
|
RubyGems 3.0.3
|
90
|
-
Rails 5.2.3
|
91
|
-
|
92
|
-
### 追記
|
93
|
-
|
94
|
-
上記の記述ですと、`rooms_index.html.erb`は正常に表示されるものの`rooms_show.html.erb`を開くと以下のエラーが表示されます。
|
95
|
-
|
96
|
-
```
|
97
|
-
NoMethodError in RoomsController#show
|
98
|
-
undefined method `update' for nil:NilClass
|
99
|
-
Extracted source (around line #51):
|
100
|
-
49 # 「message」に関する「相手からの通知」のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
|
101
|
-
50 # @notifications.find_by(room_id: params[:id]).update(read: true)
|
102
|
-
51 @current_user.passive_notifications.find_by(room_id: params[:id]).update(read: true)
|
103
|
-
52
|
104
|
-
53 # 該当のroom_id内にある未読メッセージの数を取得。→rooms_show.html.erb
|
105
|
-
54 # @unread = @notifications.find_by(room_id: params[:id]).where(read: false).count
|
106
|
-
```
|
107
|
-
|
108
|
-
またその際のログは以下の様になっています。
|
109
|
-
|
110
|
-
```
|
111
|
-
app/controllers/rooms_controller.rb:51:in `show'
|
112
|
-
Started GET "/rooms/10" for ::1 at 2020-12-15 18:36:25 +0900
|
113
|
-
Processing by RoomsController#show as HTML
|
114
|
-
Parameters: {"id"=>"10"}
|
115
|
-
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
|
116
|
-
↳ app/controllers/application_controller.rb:9
|
117
|
-
Entry Load (0.3ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
|
118
|
-
↳ app/controllers/rooms_controller.rb:24
|
119
|
-
Room Load (0.2ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
|
120
|
-
↳ app/controllers/rooms_controller.rb:31
|
121
|
-
Entry Load (0.5ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? AND "entries"."room_id" = ? [["user_id", 20], ["room_id", 10]]
|
122
|
-
↳ app/controllers/rooms_controller.rb:32
|
123
|
-
Notification Load (0.6ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."room_id" = ? LIMIT ? [["visited_id", 20], ["room_id", 10], ["LIMIT", 1]]
|
124
|
-
↳ app/controllers/rooms_controller.rb:51
|
125
|
-
Completed 500 Internal Server Error in 144ms (ActiveRecord: 8.7ms)
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
NoMethodError (undefined method `update' for nil:NilClass):
|
130
|
-
```
|
131
|
-
|
132
|
-
また、`rooms_index.html.erb`を開いた際のログはこちらです。
|
133
|
-
|
134
|
-
```
|
135
|
-
app/controllers/rooms_controller.rb:51:in `show'
|
136
|
-
Started GET "/rooms/index" for ::1 at 2020-12-15 18:41:37 +0900
|
137
|
-
Processing by RoomsController#index as HTML
|
138
|
-
User Load (2.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
|
139
|
-
↳ app/controllers/application_controller.rb:9
|
140
|
-
Entry Load (0.4ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
|
141
|
-
↳ app/controllers/rooms_controller.rb:71
|
142
|
-
Notification Load (0.5ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."checked" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["checked", 0], ["LIMIT", 7], ["OFFSET", 0]]
|
143
|
-
↳ app/controllers/rooms_controller.rb:86
|
144
|
-
(0.8ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" IS NULL AND "notifications"."read" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ?) subquery_for_count [["visited_id", 20], ["action", "message"], ["read", 0], ["LIMIT", 7], ["OFFSET", 0]]
|
145
|
-
↳ app/controllers/rooms_controller.rb:93
|
146
|
-
Rendering rooms/index.html.erb within layouts/application
|
147
|
-
Entry Load (1.6ms) SELECT "entries".* FROM "entries" WHERE "entries"."room_id" IN (?, ?) AND (user_id != 20) ORDER BY "entries"."created_at" DESC [["room_id", 10], ["room_id", 13]]
|
148
|
-
↳ app/views/rooms/index.html.erb:11
|
149
|
-
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?) [["id", 22], ["id", 13]]
|
150
|
-
↳ app/views/rooms/index.html.erb:11
|
151
|
-
Room Load (0.7ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" IN (?, ?) [["id", 13], ["id", 10]]
|
152
|
-
↳ app/views/rooms/index.html.erb:11
|
153
|
-
(0.4ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 13]]
|
154
|
-
↳ app/views/rooms/index.html.erb:30
|
155
|
-
Message Load (0.7ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT ? [["id", 55], ["LIMIT", 1]]
|
156
|
-
↳ app/views/rooms/index.html.erb:30
|
157
|
-
(0.3ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 10]]
|
158
|
-
↳ app/views/rooms/index.html.erb:30
|
159
|
-
Message Load (0.3ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" IS NULL LIMIT ? [["LIMIT", 1]]
|
160
|
-
↳ app/views/rooms/index.html.erb:30
|
161
|
-
Rendered rooms/index.html.erb within layouts/application (68.2ms)
|
162
|
-
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
|
163
|
-
↳ app/views/layouts/application.html.erb:39
|
164
|
-
(0.5ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."checked" = ? AND "notifications"."action" = ? [["visited_id", 20], ["checked", 0], ["action", "message"]]
|
165
|
-
↳ app/helpers/notifications_helper.rb:47
|
166
|
-
(1.0ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" != ? AND "notifications"."checked" = ? [["visited_id", 20], ["action", "message"], ["checked", 0]]
|
167
|
-
↳ app/helpers/notifications_helper.rb:41
|
168
|
-
Completed 200 OK in 321ms (Views: 260.2ms | ActiveRecord: 10.3ms)
|
169
|
-
|
170
|
-
|
171
|
-
Started GET "/rooms/js/tag.js" for ::1 at 2020-12-15 18:41:37 +0900
|
172
|
-
|
173
|
-
ActionController::RoutingError (No route matches [GET] "/rooms/js/tag.js"):
|
174
|
-
```
|
126
|
+
Rails 5.2.3
|
4
rooms_controller.rb内及びエラー文を最新のものに書き換え。
title
CHANGED
File without changes
|
body
CHANGED
@@ -65,8 +65,8 @@
|
|
65
65
|
def show
|
66
66
|
# ログイン中のユーザーが抱える「message」に関する相手からの通知を取得。
|
67
67
|
@notifications = @current_user.passive_notifications.where(action: 'message').includes([:visitor], [:visited]).order(created_at: :desc).page(params[:page]).per(7)
|
68
|
-
#
|
68
|
+
# ログイン中のユーザーが抱える相手からの通知のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
|
69
|
-
@
|
69
|
+
@current_user.passive_notifications.find_by(room_id: params[:id]).update(read: true)
|
70
70
|
# 該当のroom_id内にある未読メッセージの数を取得。
|
71
71
|
@unread = @notifications.where(room_id: params[:id], read: false).count
|
72
72
|
end
|
@@ -96,65 +96,79 @@
|
|
96
96
|
```
|
97
97
|
NoMethodError in RoomsController#show
|
98
98
|
undefined method `update' for nil:NilClass
|
99
|
-
|
99
|
+
Extracted source (around line #51):
|
100
|
+
49 # 「message」に関する「相手からの通知」のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
|
100
|
-
@notifications.find_by(room_id: params[:id]).update(read: true)
|
101
|
+
50 # @notifications.find_by(room_id: params[:id]).update(read: true)
|
102
|
+
51 @current_user.passive_notifications.find_by(room_id: params[:id]).update(read: true)
|
103
|
+
52
|
104
|
+
53 # 該当のroom_id内にある未読メッセージの数を取得。→rooms_show.html.erb
|
105
|
+
54 # @unread = @notifications.find_by(room_id: params[:id]).where(read: false).count
|
101
106
|
```
|
102
107
|
|
103
|
-
|
108
|
+
またその際のログは以下の様になっています。
|
104
109
|
|
105
110
|
```
|
111
|
+
app/controllers/rooms_controller.rb:51:in `show'
|
106
|
-
Started GET "/rooms/10" for ::1 at 2020-12-
|
112
|
+
Started GET "/rooms/10" for ::1 at 2020-12-15 18:36:25 +0900
|
107
113
|
Processing by RoomsController#show as HTML
|
108
114
|
Parameters: {"id"=>"10"}
|
109
|
-
User Load (0.
|
115
|
+
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
|
110
116
|
↳ app/controllers/application_controller.rb:9
|
111
|
-
Entry Load (0.
|
117
|
+
Entry Load (0.3ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
|
112
118
|
↳ app/controllers/rooms_controller.rb:24
|
113
|
-
Room Load (0.
|
119
|
+
Room Load (0.2ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
|
114
120
|
↳ app/controllers/rooms_controller.rb:31
|
115
|
-
Entry Load (0.
|
121
|
+
Entry Load (0.5ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? AND "entries"."room_id" = ? [["user_id", 20], ["room_id", 10]]
|
116
122
|
↳ app/controllers/rooms_controller.rb:32
|
117
|
-
Notification Load (0.
|
118
|
-
↳ app/controllers/rooms_controller.rb:
|
119
|
-
Completed 500 Internal Server Error in
|
123
|
+
Notification Load (0.6ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."room_id" = ? LIMIT ? [["visited_id", 20], ["room_id", 10], ["LIMIT", 1]]
|
124
|
+
↳ app/controllers/rooms_controller.rb:51
|
125
|
+
Completed 500 Internal Server Error in 144ms (ActiveRecord: 8.7ms)
|
120
126
|
|
127
|
+
|
128
|
+
|
121
129
|
NoMethodError (undefined method `update' for nil:NilClass):
|
122
130
|
```
|
123
131
|
|
124
132
|
また、`rooms_index.html.erb`を開いた際のログはこちらです。
|
125
133
|
|
126
134
|
```
|
135
|
+
app/controllers/rooms_controller.rb:51:in `show'
|
127
|
-
Started GET "/rooms/index" for ::1 at 2020-12-
|
136
|
+
Started GET "/rooms/index" for ::1 at 2020-12-15 18:41:37 +0900
|
128
137
|
Processing by RoomsController#index as HTML
|
129
|
-
User Load (
|
138
|
+
User Load (2.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
|
130
139
|
↳ app/controllers/application_controller.rb:9
|
131
|
-
Entry Load (0.
|
132
|
-
↳ app/controllers/rooms_controller.rb:
|
133
|
-
Notification Load (
|
134
|
-
↳ app/controllers/rooms_controller.rb:
|
135
|
-
(
|
136
|
-
↳ app/controllers/rooms_controller.rb:
|
140
|
+
Entry Load (0.4ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
|
141
|
+
↳ app/controllers/rooms_controller.rb:71
|
142
|
+
Notification Load (0.5ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."checked" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["checked", 0], ["LIMIT", 7], ["OFFSET", 0]]
|
143
|
+
↳ app/controllers/rooms_controller.rb:86
|
144
|
+
(0.8ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" IS NULL AND "notifications"."read" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ?) subquery_for_count [["visited_id", 20], ["action", "message"], ["read", 0], ["LIMIT", 7], ["OFFSET", 0]]
|
145
|
+
↳ app/controllers/rooms_controller.rb:93
|
137
146
|
Rendering rooms/index.html.erb within layouts/application
|
138
|
-
Entry Load (
|
147
|
+
Entry Load (1.6ms) SELECT "entries".* FROM "entries" WHERE "entries"."room_id" IN (?, ?) AND (user_id != 20) ORDER BY "entries"."created_at" DESC [["room_id", 10], ["room_id", 13]]
|
139
148
|
↳ app/views/rooms/index.html.erb:11
|
140
|
-
User Load (0.
|
149
|
+
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?) [["id", 22], ["id", 13]]
|
141
150
|
↳ app/views/rooms/index.html.erb:11
|
142
|
-
Room Load (
|
151
|
+
Room Load (0.7ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" IN (?, ?) [["id", 13], ["id", 10]]
|
143
152
|
↳ app/views/rooms/index.html.erb:11
|
144
|
-
(0.
|
153
|
+
(0.4ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 13]]
|
145
154
|
↳ app/views/rooms/index.html.erb:30
|
146
|
-
Message Load (0.
|
155
|
+
Message Load (0.7ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT ? [["id", 55], ["LIMIT", 1]]
|
147
156
|
↳ app/views/rooms/index.html.erb:30
|
148
|
-
(0.
|
157
|
+
(0.3ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 10]]
|
149
158
|
↳ app/views/rooms/index.html.erb:30
|
150
|
-
Message Load (0.
|
159
|
+
Message Load (0.3ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" IS NULL LIMIT ? [["LIMIT", 1]]
|
151
160
|
↳ app/views/rooms/index.html.erb:30
|
152
|
-
Rendered rooms/index.html.erb within layouts/application (
|
161
|
+
Rendered rooms/index.html.erb within layouts/application (68.2ms)
|
153
162
|
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
|
154
163
|
↳ app/views/layouts/application.html.erb:39
|
155
164
|
(0.5ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."checked" = ? AND "notifications"."action" = ? [["visited_id", 20], ["checked", 0], ["action", "message"]]
|
156
165
|
↳ app/helpers/notifications_helper.rb:47
|
157
|
-
(
|
166
|
+
(1.0ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" != ? AND "notifications"."checked" = ? [["visited_id", 20], ["action", "message"], ["checked", 0]]
|
158
167
|
↳ app/helpers/notifications_helper.rb:41
|
159
|
-
Completed 200 OK in
|
168
|
+
Completed 200 OK in 321ms (Views: 260.2ms | ActiveRecord: 10.3ms)
|
169
|
+
|
170
|
+
|
171
|
+
Started GET "/rooms/js/tag.js" for ::1 at 2020-12-15 18:41:37 +0900
|
172
|
+
|
173
|
+
ActionController::RoutingError (No route matches [GET] "/rooms/js/tag.js"):
|
160
174
|
```
|
3
「追記」を記入
title
CHANGED
File without changes
|
body
CHANGED
@@ -66,10 +66,15 @@
|
|
66
66
|
# ログイン中のユーザーが抱える「message」に関する相手からの通知を取得。
|
67
67
|
@notifications = @current_user.passive_notifications.where(action: 'message').includes([:visitor], [:visited]).order(created_at: :desc).page(params[:page]).per(7)
|
68
68
|
# 「message」に関する相手からの通知のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
|
69
|
-
@notifications.find_by(room_id: params[:id]).
|
69
|
+
@notifications.find_by(room_id: params[:id]).update(read: true)
|
70
70
|
# 該当のroom_id内にある未読メッセージの数を取得。
|
71
|
-
@unread = @notifications.
|
71
|
+
@unread = @notifications.where(room_id: params[:id], read: false).count
|
72
72
|
end
|
73
|
+
|
74
|
+
def index
|
75
|
+
@unread = @notifications.where(room_id: params[:id], read: false).count
|
76
|
+
end
|
77
|
+
|
73
78
|
```
|
74
79
|
|
75
80
|
### rooms_index.html.erb
|
@@ -82,4 +87,74 @@
|
|
82
87
|
|
83
88
|
ruby 2.6.4p104
|
84
89
|
RubyGems 3.0.3
|
85
|
-
Rails 5.2.3
|
90
|
+
Rails 5.2.3
|
91
|
+
|
92
|
+
### 追記
|
93
|
+
|
94
|
+
上記の記述ですと、`rooms_index.html.erb`は正常に表示されるものの`rooms_show.html.erb`を開くと以下のエラーが表示されます。
|
95
|
+
|
96
|
+
```
|
97
|
+
NoMethodError in RoomsController#show
|
98
|
+
undefined method `update' for nil:NilClass
|
99
|
+
|
100
|
+
@notifications.find_by(room_id: params[:id]).update(read: true)
|
101
|
+
```
|
102
|
+
|
103
|
+
何かパラメータが上手く渡っていないのか…。その際のログは以下の様になっています。
|
104
|
+
|
105
|
+
```
|
106
|
+
Started GET "/rooms/10" for ::1 at 2020-12-13 23:19:49 +0900
|
107
|
+
Processing by RoomsController#show as HTML
|
108
|
+
Parameters: {"id"=>"10"}
|
109
|
+
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
|
110
|
+
↳ app/controllers/application_controller.rb:9
|
111
|
+
Entry Load (0.4ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
|
112
|
+
↳ app/controllers/rooms_controller.rb:24
|
113
|
+
Room Load (0.4ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" = ? LIMIT ? [["id", 10], ["LIMIT", 1]]
|
114
|
+
↳ app/controllers/rooms_controller.rb:31
|
115
|
+
Entry Load (0.7ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? AND "entries"."room_id" = ? [["user_id", 20], ["room_id", 10]]
|
116
|
+
↳ app/controllers/rooms_controller.rb:32
|
117
|
+
Notification Load (0.7ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["room_id", 10], ["LIMIT", 1], ["OFFSET", 0]]
|
118
|
+
↳ app/controllers/rooms_controller.rb:50
|
119
|
+
Completed 500 Internal Server Error in 143ms (ActiveRecord: 10.4ms)
|
120
|
+
|
121
|
+
NoMethodError (undefined method `update' for nil:NilClass):
|
122
|
+
```
|
123
|
+
|
124
|
+
また、`rooms_index.html.erb`を開いた際のログはこちらです。
|
125
|
+
|
126
|
+
```
|
127
|
+
Started GET "/rooms/index" for ::1 at 2020-12-13 23:23:45 +0900
|
128
|
+
Processing by RoomsController#index as HTML
|
129
|
+
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
|
130
|
+
↳ app/controllers/application_controller.rb:9
|
131
|
+
Entry Load (0.2ms) SELECT "entries".* FROM "entries" WHERE "entries"."user_id" = ? [["user_id", 20]]
|
132
|
+
↳ app/controllers/rooms_controller.rb:70
|
133
|
+
Notification Load (3.0ms) SELECT "notifications".* FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."checked" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ? [["visited_id", 20], ["action", "message"], ["checked", 0], ["LIMIT", 7], ["OFFSET", 0]]
|
134
|
+
↳ app/controllers/rooms_controller.rb:85
|
135
|
+
(2.2ms) SELECT COUNT(*) FROM (SELECT 1 AS one FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" = ? AND "notifications"."room_id" IS NULL AND "notifications"."read" = ? ORDER BY "notifications"."created_at" DESC LIMIT ? OFFSET ?) subquery_for_count [["visited_id", 20], ["action", "message"], ["read", 0], ["LIMIT", 7], ["OFFSET", 0]]
|
136
|
+
↳ app/controllers/rooms_controller.rb:92
|
137
|
+
Rendering rooms/index.html.erb within layouts/application
|
138
|
+
Entry Load (0.5ms) SELECT "entries".* FROM "entries" WHERE "entries"."room_id" IN (?, ?) AND (user_id != 20) ORDER BY "entries"."created_at" DESC [["room_id", 10], ["room_id", 13]]
|
139
|
+
↳ app/views/rooms/index.html.erb:11
|
140
|
+
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (?, ?) [["id", 22], ["id", 13]]
|
141
|
+
↳ app/views/rooms/index.html.erb:11
|
142
|
+
Room Load (3.4ms) SELECT "rooms".* FROM "rooms" WHERE "rooms"."id" IN (?, ?) [["id", 13], ["id", 10]]
|
143
|
+
↳ app/views/rooms/index.html.erb:11
|
144
|
+
(0.3ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 13]]
|
145
|
+
↳ app/views/rooms/index.html.erb:30
|
146
|
+
Message Load (0.4ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" = ? LIMIT ? [["id", 55], ["LIMIT", 1]]
|
147
|
+
↳ app/views/rooms/index.html.erb:30
|
148
|
+
(0.4ms) SELECT "messages"."id" FROM "messages" WHERE "messages"."room_id" = ? [["room_id", 10]]
|
149
|
+
↳ app/views/rooms/index.html.erb:30
|
150
|
+
Message Load (0.2ms) SELECT "messages".* FROM "messages" WHERE "messages"."id" IS NULL LIMIT ? [["LIMIT", 1]]
|
151
|
+
↳ app/views/rooms/index.html.erb:30
|
152
|
+
Rendered rooms/index.html.erb within layouts/application (42.9ms)
|
153
|
+
CACHE User Load (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 20], ["LIMIT", 1]]
|
154
|
+
↳ app/views/layouts/application.html.erb:39
|
155
|
+
(0.5ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."checked" = ? AND "notifications"."action" = ? [["visited_id", 20], ["checked", 0], ["action", "message"]]
|
156
|
+
↳ app/helpers/notifications_helper.rb:47
|
157
|
+
(0.6ms) SELECT COUNT(*) FROM "notifications" WHERE "notifications"."visited_id" = ? AND "notifications"."action" != ? AND "notifications"."checked" = ? [["visited_id", 20], ["action", "message"], ["checked", 0]]
|
158
|
+
↳ app/helpers/notifications_helper.rb:41
|
159
|
+
Completed 200 OK in 371ms (Views: 325.4ms | ActiveRecord: 13.1ms)
|
160
|
+
```
|
2
rooms_controller.rb及びrooms_index.html.erbを現在の状態に書き直し。
title
CHANGED
File without changes
|
body
CHANGED
@@ -61,32 +61,21 @@
|
|
61
61
|
|
62
62
|
### rooms_controller.rb
|
63
63
|
|
64
|
-
この記述ですと、どの`room`を開いても全ての`message`レコードが`read: true`になってしまいます…。
|
65
64
|
```Ruby
|
66
65
|
def show
|
67
|
-
# ログイン中のユーザーが抱える「message」に関する
|
66
|
+
# ログイン中のユーザーが抱える「message」に関する相手からの通知を取得。
|
68
67
|
@notifications = @current_user.passive_notifications.where(action: 'message').includes([:visitor], [:visited]).order(created_at: :desc).page(params[:page]).per(7)
|
69
|
-
@notifications.where(read: false).each do |notification|
|
70
|
-
|
68
|
+
# 「message」に関する相手からの通知のうち、該当のroom_idを取得し、showを開いた時点でreadをtrueにする。
|
71
|
-
|
69
|
+
@notifications.find_by(room_id: params[:id]).update_attributes(read: true)
|
72
|
-
|
70
|
+
# 該当のroom_id内にある未読メッセージの数を取得。
|
71
|
+
@unread = @notifications.find_by(room_id: params[:id]).where(read: false, action: 'message').count
|
73
72
|
end
|
74
73
|
```
|
75
74
|
|
76
|
-
### notifications_helper.rb
|
77
|
-
|
78
|
-
この記述ですと、未読メッセージが全部でいくつあるかが取得され、各room内における個別の件数が分かりません。
|
79
|
-
```Ruby
|
80
|
-
#ログイン中のユーザーに、「message」に関する未読(=read: false)の通知が「いくつあるか」を判別。
|
81
|
-
def unread_message_notifications_count
|
82
|
-
@unread = @current_user.passive_notifications.where(read: false, action: 'message').count
|
83
|
-
end
|
84
|
-
```
|
85
|
-
|
86
75
|
### rooms_index.html.erb
|
87
76
|
|
88
77
|
```Rails
|
89
|
-
<%=
|
78
|
+
<%= @unread %>
|
90
79
|
```
|
91
80
|
|
92
81
|
### 補足情報(FW/ツールのバージョンなど)
|
1
helper.rb内、index.html.erb内の記述を編集。
title
CHANGED
File without changes
|
body
CHANGED
@@ -77,18 +77,16 @@
|
|
77
77
|
|
78
78
|
この記述ですと、未読メッセージが全部でいくつあるかが取得され、各room内における個別の件数が分かりません。
|
79
79
|
```Ruby
|
80
|
-
|
80
|
+
#ログイン中のユーザーに、「message」に関する未読(=read: false)の通知が「いくつあるか」を判別。
|
81
|
-
|
81
|
+
def unread_message_notifications_count
|
82
|
-
|
82
|
+
@unread = @current_user.passive_notifications.where(read: false, action: 'message').count
|
83
|
-
|
83
|
+
end
|
84
84
|
```
|
85
85
|
|
86
86
|
### rooms_index.html.erb
|
87
87
|
|
88
88
|
```Rails
|
89
|
-
<% if message_id.notification.read = 'false' %>
|
90
|
-
<%=
|
89
|
+
<%= unread_message_notifications_count %>
|
91
|
-
<% end %>
|
92
90
|
```
|
93
91
|
|
94
92
|
### 補足情報(FW/ツールのバージョンなど)
|