質問編集履歴

4

エラーファイルの変更、書き換えたコードの追加

2020/02/21 12:37

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -18,15 +18,67 @@
18
18
 
19
19
  Showing /Users/yuuka/projects/wedding-app/app/views/likes/_like.html.haml where line #2 raised:
20
20
 
21
+
22
+
21
- undefined method `like_user' for nil:NilClass
23
+ undefined method `like_user' for #<Hall::ActiveRecord_Relation:0x00007fd0d2843c80>
24
+
25
+ Extracted source (around line #2):
26
+
27
+
28
+
29
+ 1- if user_signed_in?
30
+
31
+ 2 - if halls.likes_user(current_user.id)
32
+
33
+ ```
34
+
35
+
36
+
37
+ ### 該当のソースコード
38
+
39
+
40
+
41
+ ```ruby
42
+
43
+ # app/models/hall.rb
44
+
45
+ class Hall < ApplicationRecord
46
+
47
+ belongs_to :user
48
+
49
+ has_many :likes, dependent: :destroy
50
+
51
+
52
+
53
+ def like_user(user_id)
54
+
55
+ likes.find_by(user_id: user_id)
56
+
57
+ end
58
+
59
+ end
60
+
61
+
62
+
63
+
64
+
65
+
66
+
67
+ ```
68
+
69
+
70
+
71
+ ```haml
72
+
73
+ # views/likes/_like.html.haml
22
74
 
23
75
 
24
76
 
25
77
  - if user_signed_in?
26
78
 
27
- - if hall.like_user(current_user.id) #ここでエラーが起きています
79
+ - if halls.like_user(current_user.id)
28
-
80
+
29
- .item-button-container__left__dislike
81
+ .hall__name__dislike
30
82
 
31
83
  =link_to user_hall_like_path(current_user, hall, like), method: "DELETE", remote: true do
32
84
 
@@ -34,129 +86,131 @@
34
86
 
35
87
  %span いいね!
36
88
 
37
- ```
89
+ %span
38
-
39
- ```
90
+
40
-
41
- NoMethodError in Halls#index
42
-
43
- Showing /Users/yuuka/projects/wedding-app/app/views/likes/_like.html.haml where line #2 raised:
44
-
45
-
46
-
47
- undefined method `like_user' for nil:NilClass
48
-
49
- Extracted source (around line #2):
50
-
51
-
52
-
53
- - if user_signed_in?
91
+ = h.likes_count
54
-
92
+
55
- - if hall.like_user(current_user.id)
93
+ - else
56
-
94
+
57
- .item-button-container__left__dislike
95
+ .hall__name__like
58
-
96
+
59
- =link_to user_hall_like_path(current_user, hall, like), method: "DELETE", remote: true do
97
+ =link_to user_hall_likes_path(current_user, hall), method: "POST", remote: true do
60
98
 
61
99
  %i.fas.fa-heart
62
100
 
63
101
  %span いいね!
64
102
 
103
+ %span
104
+
105
+ = h.likes_count
106
+
107
+ -else
108
+
109
+ .hall__name__like
110
+
111
+ %i.fas.fa-heart
112
+
113
+ %span いいね!
114
+
115
+ %span
116
+
117
+ = h.like_count
118
+
65
- ```
119
+ ```
66
-
67
- ### 該当のソースコード
68
-
69
-
70
120
 
71
121
  ```ruby
72
122
 
73
- # app/models/hall.rb
123
+ # halls_controller
74
-
124
+
125
+
126
+
75
- class Hall < ApplicationRecord
127
+ class HallsController < ApplicationController
128
+
76
-
129
+ def index
130
+
77
- belongs_to :user
131
+ @halls = Hall.all
78
-
79
- has_many :likes, dependent: :destroy
132
+
80
-
81
-
82
-
83
- def like_user(user_id)
84
-
85
- likes.find_by(user_id: user_id)
86
-
87
- end
133
+ end
134
+
135
+
88
136
 
89
137
  end
90
138
 
91
-
92
-
93
-
94
-
95
-
96
-
97
- ```
139
+ ```
98
-
99
-
100
140
 
101
141
  ```haml
102
142
 
103
- # views/likes/_like.html.haml
104
-
105
-
106
-
107
- - if user_signed_in?
108
-
109
- - if hall.like_user(current_user.id)
110
-
111
- .item-button-container__left__dislike
112
-
113
- =link_to user_hall_like_path(current_user, hall, like), method: "DELETE", remote: true do
114
-
115
- %i.fas.fa-heart
116
-
117
- %span いいね!
118
-
119
- %span
120
-
121
- = hall.likes_count
122
-
123
- - else
124
-
125
- .item-button-container__left__like
126
-
127
- =link_to user_hall_likes_path(current_user, hall), method: "POST", remote: true do
128
-
129
- %i.fas.fa-heart
130
-
131
- %span いいね!
132
-
133
- %span
134
-
135
- = hall.likes_count
136
-
137
- -else
138
-
139
- .item-button-container__left__like
140
-
141
- %i.fas.fa-heart
142
-
143
- %span いいね!
144
-
145
- %span = hall.like_count
146
-
147
- ```
148
-
149
- ```ruby
150
-
151
- # halls_controller
152
-
153
-
154
-
155
- class HallsController < ApplicationController
156
-
157
- def index
158
-
159
- @hall = Hall.find_by(id: params[:id])
143
+
144
+
145
+ # views/halls/index.html.haml
146
+
147
+
148
+
149
+ hall
150
+
151
+ - if @halls.present?
152
+
153
+ - @halls.each do |h|
154
+
155
+ .hall__name
156
+
157
+ = h.name
158
+
159
+ = render partial: 'likes/like', locals: { halls: @halls, likes: @likes, like: @like}
160
+
161
+ .hall__image
162
+
163
+ = image_tag h.image
164
+
165
+ .hall__place
166
+
167
+ = h.place
168
+
169
+ .hall__link
170
+
171
+ = link_to top_page_index_path(current_user.id) do
172
+
173
+ 詳細をみる
174
+
175
+ ```
176
+
177
+ ```
178
+
179
+ # controller/likes_controller
180
+
181
+ class LikesController < ApplicationController
182
+
183
+
184
+
185
+ def create
186
+
187
+ @like = Like.create(user_id: current_user.id, hall_id: params[:hall_id])
188
+
189
+ @likes = Like.where(hall_id: params[:hall_id])
190
+
191
+ get_hall
192
+
193
+ end
194
+
195
+
196
+
197
+ def destroy
198
+
199
+ @like = Like.find_by(user_id: current_user.id, hall_id: params[:hall_id])
200
+
201
+ @like.destroy
202
+
203
+ @likes = Like.where(hall_id: params[:hall_id])
204
+
205
+ get_hall
206
+
207
+ end
208
+
209
+
210
+
211
+ def get_hall
212
+
213
+ @hall = Hall.find(params[:hall_id])
160
214
 
161
215
  end
162
216
 
@@ -166,105 +220,17 @@
166
220
 
167
221
  ```
168
222
 
169
- ```haml
170
-
171
-
172
-
173
- # views/halls/index.html.haml
174
-
175
-
176
-
177
- .hall
178
-
179
- - if @hall.present?
180
-
181
- - @hall.each do |h|
182
-
183
- .hall__name
184
-
185
- = h.name
186
-
187
- .hall__image
188
-
189
- = image_tag h.image
190
-
191
- .hall__place
192
-
193
- = h.place
194
-
195
- .hall__link
196
-
197
- = link_to top_page_index_path(current_user.id) do
198
-
199
- 詳細をみる
200
-
201
-
202
-
203
- .item-button-container
204
-
205
- .item-button-container__left
206
-
207
- = render partial: 'likes/like', locals: { hall: @hall, halls: @halls, likes: @likes, like: @like}
208
-
209
- ```
210
-
211
- ```
212
-
213
- # controller/likes_controller
214
-
215
- class LikesController < ApplicationController
216
-
217
-
218
-
219
- def create
220
-
221
- @like = Like.create(user_id: current_user.id, hall_id: params[:hall_id])
222
-
223
- @likes = Like.where(hall_id: params[:hall_id])
224
-
225
- get_hall
226
-
227
- end
228
-
229
-
230
-
231
- def destroy
232
-
233
- @like = Like.find_by(user_id: current_user.id, hall_id: params[:hall_id])
234
-
235
- @like.destroy
236
-
237
- @likes = Like.where(hall_id: params[:hall_id])
238
-
239
- get_hall
240
-
241
- end
242
-
243
-
244
-
245
- def get_hall
246
-
247
- @hall = Hall.find(params[:hall_id])
248
-
249
- end
250
-
251
-
252
-
253
- end
254
-
255
- ```
256
-
257
223
  ### 試したこと
258
224
 
259
- def like_user(user_id)
225
+ - if halls.like_user(current_user.id)
260
-
226
+
261
- likes.find_by(user_id: user_id)
227
+ hallsの中には@halls.each do |h|の情報が入っており、
262
-
228
+
263
- end
229
+ current_user.idも情報が入っていますがエラーが起きます
264
-
230
+
265
- ここうま読み取ていないのではないか
231
+ like_userのなかに情報入ってくれないのではないか
266
-
267
- ファイルを写したりしましたが変化はありませんでした。
232
+
233
+
268
234
 
269
235
  ### 補足情報(FW/ツールのバージョンなど)
270
236
 

3

エラーファイルの追加

2020/02/21 12:37

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -36,7 +36,33 @@
36
36
 
37
37
  ```
38
38
 
39
-
39
+ ```
40
+
41
+ NoMethodError in Halls#index
42
+
43
+ Showing /Users/yuuka/projects/wedding-app/app/views/likes/_like.html.haml where line #2 raised:
44
+
45
+
46
+
47
+ undefined method `like_user' for nil:NilClass
48
+
49
+ Extracted source (around line #2):
50
+
51
+
52
+
53
+ - if user_signed_in?
54
+
55
+ - if hall.like_user(current_user.id)
56
+
57
+ .item-button-container__left__dislike
58
+
59
+ =link_to user_hall_like_path(current_user, hall, like), method: "DELETE", remote: true do
60
+
61
+ %i.fas.fa-heart
62
+
63
+ %span いいね!
64
+
65
+ ```
40
66
 
41
67
  ### 該当のソースコード
42
68
 

2

likes_controllerの追加

2020/02/19 05:23

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -182,7 +182,51 @@
182
182
 
183
183
  ```
184
184
 
185
-
185
+ ```
186
+
187
+ # controller/likes_controller
188
+
189
+ class LikesController < ApplicationController
190
+
191
+
192
+
193
+ def create
194
+
195
+ @like = Like.create(user_id: current_user.id, hall_id: params[:hall_id])
196
+
197
+ @likes = Like.where(hall_id: params[:hall_id])
198
+
199
+ get_hall
200
+
201
+ end
202
+
203
+
204
+
205
+ def destroy
206
+
207
+ @like = Like.find_by(user_id: current_user.id, hall_id: params[:hall_id])
208
+
209
+ @like.destroy
210
+
211
+ @likes = Like.where(hall_id: params[:hall_id])
212
+
213
+ get_hall
214
+
215
+ end
216
+
217
+
218
+
219
+ def get_hall
220
+
221
+ @hall = Hall.find(params[:hall_id])
222
+
223
+ end
224
+
225
+
226
+
227
+ end
228
+
229
+ ```
186
230
 
187
231
  ### 試したこと
188
232
 

1

controllerをHall.find_by(id: params[:id]) に変更するとnil:NilClassと言うエラー文に変わりました。

2020/02/19 04:00

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  Showing /Users/yuuka/projects/wedding-app/app/views/likes/_like.html.haml where line #2 raised:
20
20
 
21
- undefined method `like_user' for #<Hall::ActiveRecord_Relation:0x00007fd0cd400088>
21
+ undefined method `like_user' for nil:NilClass
22
22
 
23
23
 
24
24
 
@@ -120,6 +120,68 @@
120
120
 
121
121
  ```
122
122
 
123
+ ```ruby
124
+
125
+ # halls_controller
126
+
127
+
128
+
129
+ class HallsController < ApplicationController
130
+
131
+ def index
132
+
133
+ @hall = Hall.find_by(id: params[:id])
134
+
135
+ end
136
+
137
+
138
+
139
+ end
140
+
141
+ ```
142
+
143
+ ```haml
144
+
145
+
146
+
147
+ # views/halls/index.html.haml
148
+
149
+
150
+
151
+ .hall
152
+
153
+ - if @hall.present?
154
+
155
+ - @hall.each do |h|
156
+
157
+ .hall__name
158
+
159
+ = h.name
160
+
161
+ .hall__image
162
+
163
+ = image_tag h.image
164
+
165
+ .hall__place
166
+
167
+ = h.place
168
+
169
+ .hall__link
170
+
171
+ = link_to top_page_index_path(current_user.id) do
172
+
173
+ 詳細をみる
174
+
175
+
176
+
177
+ .item-button-container
178
+
179
+ .item-button-container__left
180
+
181
+ = render partial: 'likes/like', locals: { hall: @hall, halls: @halls, likes: @likes, like: @like}
182
+
183
+ ```
184
+
123
185
 
124
186
 
125
187
  ### 試したこと