質問編集履歴
2
モデル追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -223,3 +223,81 @@
|
|
223
223
|
エラーが少し変わって下記のようになりました。
|
224
224
|
|
225
225
|
![イメージ説明](8262016f858327d76ebe80dcb4bee5ac.png)
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
---
|
230
|
+
|
231
|
+
**【9/27更新】**
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
モデルのコードは下記となります。上から「Message」「User」「Like」です。
|
236
|
+
|
237
|
+
```ここに言語を入力
|
238
|
+
|
239
|
+
class Message < ApplicationRecord
|
240
|
+
|
241
|
+
belongs_to :user
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
validates :content, presence: true
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
has_many :likes
|
250
|
+
|
251
|
+
has_many :liked_users, through: :likes, source: :user
|
252
|
+
|
253
|
+
end
|
254
|
+
|
255
|
+
```
|
256
|
+
|
257
|
+
```ここに言語を入力
|
258
|
+
|
259
|
+
class User < ApplicationRecord
|
260
|
+
|
261
|
+
# Include default devise modules. Others available are:
|
262
|
+
|
263
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
264
|
+
|
265
|
+
devise :database_authenticatable, :registerable,
|
266
|
+
|
267
|
+
:recoverable, :rememberable, :validatable
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
validates :name, presence: true, uniqueness: true
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
has_many :messages
|
276
|
+
|
277
|
+
has_many :likes
|
278
|
+
|
279
|
+
has_many :liked_messages, through: :likes, source: :message
|
280
|
+
|
281
|
+
def already_liked?(message)
|
282
|
+
|
283
|
+
self.likes.exists?(message_id: message.id)
|
284
|
+
|
285
|
+
end
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
```
|
290
|
+
|
291
|
+
```ここに言語を入力
|
292
|
+
|
293
|
+
class Like < ApplicationRecord
|
294
|
+
|
295
|
+
belongs_to :message
|
296
|
+
|
297
|
+
belongs_to :user
|
298
|
+
|
299
|
+
validates_uniqueness_of :message_id, scope: :user_id
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
```
|
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -101,3 +101,125 @@
|
|
101
101
|
|
102
102
|
|
103
103
|
アドバイスを頂ければ幸いでございます。
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
---
|
108
|
+
|
109
|
+
**【9/26更新】**
|
110
|
+
|
111
|
+
上記エラー文に「undefined method `message_likes_path'」 とあったので、pathを調べるために、rails routesを確認しました。
|
112
|
+
|
113
|
+
```ここに言語を入力
|
114
|
+
|
115
|
+
Prefix Verb URI Pattern Controller#Action
|
116
|
+
|
117
|
+
new_user_session GET /users/sign_in(.:format) devise/sessions#new
|
118
|
+
|
119
|
+
user_session POST /users/sign_in(.:format) devise/sessions#create
|
120
|
+
|
121
|
+
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
|
122
|
+
|
123
|
+
new_user_password GET /users/password/new(.:format) devise/passwords#new
|
124
|
+
|
125
|
+
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
|
126
|
+
|
127
|
+
user_password PATCH /users/password(.:format) devise/passwords#update
|
128
|
+
|
129
|
+
PUT /users/password(.:format) devise/passwords#update
|
130
|
+
|
131
|
+
POST /users/password(.:format) devise/passwords#create
|
132
|
+
|
133
|
+
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
|
134
|
+
|
135
|
+
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
|
136
|
+
|
137
|
+
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
|
138
|
+
|
139
|
+
user_registration PATCH /users(.:format) devise/registrations#update
|
140
|
+
|
141
|
+
PUT /users(.:format) devise/registrations#update
|
142
|
+
|
143
|
+
DELETE /users(.:format) devise/registrations#destroy
|
144
|
+
|
145
|
+
POST /users(.:format) devise/registrations#create
|
146
|
+
|
147
|
+
root GET / messages#index
|
148
|
+
|
149
|
+
user_message_likes POST /users/:user_id/messages/:message_id/likes(.:format) likes#create
|
150
|
+
|
151
|
+
user_message_like DELETE /users/:user_id/messages/:message_id/likes/:id(.:format) likes#destroy
|
152
|
+
|
153
|
+
user_messages GET /users/:user_id/messages(.:format) messages#index
|
154
|
+
|
155
|
+
POST /users/:user_id/messages(.:format) messages#create
|
156
|
+
|
157
|
+
edit_user GET /users/:id/edit(.:format) users#edit
|
158
|
+
|
159
|
+
user PATCH /users/:id(.:format) users#update
|
160
|
+
|
161
|
+
PUT /users/:id(.:format) users#update
|
162
|
+
|
163
|
+
```
|
164
|
+
|
165
|
+
prefixに「user_message_likes」とあったので、「message_likes_path」を「user_message_likes_path」に変更しました。また追加で、引数を最上部の配列の「m」に変更し、viewファイルを下記のようにしました。
|
166
|
+
|
167
|
+
```ここに言語を入力
|
168
|
+
|
169
|
+
.messages
|
170
|
+
|
171
|
+
- @messages.each do |m|
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
.message
|
176
|
+
|
177
|
+
.takerface
|
178
|
+
|
179
|
+
%img{alt: "userface", src: "https://imgc.eximg.jp/i=https%253A%252F%252Fs.eximg.jp%252Fexnews%252Ffeed%252FRen_ai%252FRen_ai_188420_6aed_1.jpg,zoom=600,quality=70,type=jpg"}/
|
180
|
+
|
181
|
+
.contents
|
182
|
+
|
183
|
+
.takername
|
184
|
+
|
185
|
+
= m.user.name + " さんへ"
|
186
|
+
|
187
|
+
.letter
|
188
|
+
|
189
|
+
- if m.content.present?
|
190
|
+
|
191
|
+
%p.lower-message__content
|
192
|
+
|
193
|
+
= m.content
|
194
|
+
|
195
|
+
.credo
|
196
|
+
|
197
|
+
#仕事好き #親身心 #人生を変える
|
198
|
+
|
199
|
+
.like
|
200
|
+
|
201
|
+
%h3
|
202
|
+
|
203
|
+
いいね件数: #{m.likes.count}
|
204
|
+
|
205
|
+
- if current_user.already_liked?(m)
|
206
|
+
|
207
|
+
= button_to 'いいねを取り消す', user_message_like_path(m), method: :delete
|
208
|
+
|
209
|
+
- else
|
210
|
+
|
211
|
+
= button_to 'いいね', user_message_likes_path(m)
|
212
|
+
|
213
|
+
%h2 いいねしたユーザー
|
214
|
+
|
215
|
+
- m.liked_users.each do |user|
|
216
|
+
|
217
|
+
%li= user.email
|
218
|
+
|
219
|
+
= link_to "ホームへ戻る", messages_path
|
220
|
+
|
221
|
+
```
|
222
|
+
|
223
|
+
エラーが少し変わって下記のようになりました。
|
224
|
+
|
225
|
+
![イメージ説明](8262016f858327d76ebe80dcb4bee5ac.png)
|