teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

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

2020/02/21 12:37

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -8,29 +8,14 @@
8
8
  ```
9
9
  NoMethodError in Halls#index
10
10
  Showing /Users/yuuka/projects/wedding-app/app/views/likes/_like.html.haml where line #2 raised:
11
- undefined method `like_user' for nil:NilClass
12
11
 
12
+ undefined method `like_user' for #<Hall::ActiveRecord_Relation:0x00007fd0d2843c80>
13
+ Extracted source (around line #2):
14
+
13
- - if user_signed_in?
15
+ - if user_signed_in?
14
- - if hall.like_user(current_user.id) #ここでエラーが起きています
16
+ - if halls.likes_user(current_user.id)
15
- .item-button-container__left__dislike
16
- =link_to user_hall_like_path(current_user, hall, like), method: "DELETE", remote: true do
17
- %i.fas.fa-heart
18
- %span いいね!
19
17
  ```
20
- ```
21
- NoMethodError in Halls#index
22
- Showing /Users/yuuka/projects/wedding-app/app/views/likes/_like.html.haml where line #2 raised:
23
18
 
24
- undefined method `like_user' for nil:NilClass
25
- Extracted source (around line #2):
26
-
27
- - if user_signed_in?
28
- - if hall.like_user(current_user.id)
29
- .item-button-container__left__dislike
30
- =link_to user_hall_like_path(current_user, hall, like), method: "DELETE", remote: true do
31
- %i.fas.fa-heart
32
- %span いいね!
33
- ```
34
19
  ### 該当のソースコード
35
20
 
36
21
  ```ruby
@@ -52,32 +37,33 @@
52
37
  # views/likes/_like.html.haml
53
38
 
54
39
  - if user_signed_in?
55
- - if hall.like_user(current_user.id)
40
+ - if halls.like_user(current_user.id)
56
- .item-button-container__left__dislike
41
+ .hall__name__dislike
57
42
  =link_to user_hall_like_path(current_user, hall, like), method: "DELETE", remote: true do
58
43
  %i.fas.fa-heart
59
44
  %span いいね!
60
45
  %span
61
- = hall.likes_count
46
+ = h.likes_count
62
47
  - else
63
- .item-button-container__left__like
48
+ .hall__name__like
64
49
  =link_to user_hall_likes_path(current_user, hall), method: "POST", remote: true do
65
50
  %i.fas.fa-heart
66
51
  %span いいね!
67
52
  %span
68
- = hall.likes_count
53
+ = h.likes_count
69
54
  -else
70
- .item-button-container__left__like
55
+ .hall__name__like
71
56
  %i.fas.fa-heart
72
57
  %span いいね!
58
+ %span
73
- %span = hall.like_count
59
+ = h.like_count
74
60
  ```
75
61
  ```ruby
76
62
  # halls_controller
77
63
 
78
64
  class HallsController < ApplicationController
79
65
  def index
80
- @hall = Hall.find_by(id: params[:id])
66
+ @halls = Hall.all
81
67
  end
82
68
 
83
69
  end
@@ -86,11 +72,12 @@
86
72
 
87
73
  # views/halls/index.html.haml
88
74
 
89
- .hall
75
+ hall
90
- - if @hall.present?
76
+ - if @halls.present?
91
- - @hall.each do |h|
77
+ - @halls.each do |h|
92
78
  .hall__name
93
79
  = h.name
80
+ = render partial: 'likes/like', locals: { halls: @halls, likes: @likes, like: @like}
94
81
  .hall__image
95
82
  = image_tag h.image
96
83
  .hall__place
@@ -98,10 +85,6 @@
98
85
  .hall__link
99
86
  = link_to top_page_index_path(current_user.id) do
100
87
  詳細をみる
101
-
102
- .item-button-container
103
- .item-button-container__left
104
- = render partial: 'likes/like', locals: { hall: @hall, halls: @halls, likes: @likes, like: @like}
105
88
  ```
106
89
  ```
107
90
  # controller/likes_controller
@@ -127,11 +110,11 @@
127
110
  end
128
111
  ```
129
112
  ### 試したこと
130
- def like_user(user_id)
113
+ - if halls.like_user(current_user.id)
131
- likes.find_by(user_id: user_id)
114
+ hallsの中には@halls.each do |h|の情報が入っており、
132
- end
115
+ current_user.idも情報が入っていますがエラーが起きます
133
- ここうま読み取ていないのではないか
116
+ like_userのなかに情報入ってくれないのではないか
134
- ファイルを写したりしましたが変化はありませんでした。
117
+
135
118
  ### 補足情報(FW/ツールのバージョンなど)
136
119
  このサイトを真似してみました
137
120
  https://qiita.com/haruya_hamasaki/items/75d41a1aafb87408737d

3

エラーファイルの追加

2020/02/21 12:37

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -17,7 +17,20 @@
17
17
  %i.fas.fa-heart
18
18
  %span いいね!
19
19
  ```
20
+ ```
21
+ NoMethodError in Halls#index
22
+ Showing /Users/yuuka/projects/wedding-app/app/views/likes/_like.html.haml where line #2 raised:
20
23
 
24
+ undefined method `like_user' for nil:NilClass
25
+ Extracted source (around line #2):
26
+
27
+ - if user_signed_in?
28
+ - if hall.like_user(current_user.id)
29
+ .item-button-container__left__dislike
30
+ =link_to user_hall_like_path(current_user, hall, like), method: "DELETE", remote: true do
31
+ %i.fas.fa-heart
32
+ %span いいね!
33
+ ```
21
34
  ### 該当のソースコード
22
35
 
23
36
  ```ruby

2

likes_controllerの追加

2020/02/19 05:23

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -90,7 +90,29 @@
90
90
  .item-button-container__left
91
91
  = render partial: 'likes/like', locals: { hall: @hall, halls: @halls, likes: @likes, like: @like}
92
92
  ```
93
+ ```
94
+ # controller/likes_controller
95
+ class LikesController < ApplicationController
93
96
 
97
+ def create
98
+ @like = Like.create(user_id: current_user.id, hall_id: params[:hall_id])
99
+ @likes = Like.where(hall_id: params[:hall_id])
100
+ get_hall
101
+ end
102
+
103
+ def destroy
104
+ @like = Like.find_by(user_id: current_user.id, hall_id: params[:hall_id])
105
+ @like.destroy
106
+ @likes = Like.where(hall_id: params[:hall_id])
107
+ get_hall
108
+ end
109
+
110
+ def get_hall
111
+ @hall = Hall.find(params[:hall_id])
112
+ end
113
+
114
+ end
115
+ ```
94
116
  ### 試したこと
95
117
  def like_user(user_id)
96
118
  likes.find_by(user_id: user_id)

1

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

2020/02/19 04:00

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -8,7 +8,7 @@
8
8
  ```
9
9
  NoMethodError in Halls#index
10
10
  Showing /Users/yuuka/projects/wedding-app/app/views/likes/_like.html.haml where line #2 raised:
11
- undefined method `like_user' for #<Hall::ActiveRecord_Relation:0x00007fd0cd400088>
11
+ undefined method `like_user' for nil:NilClass
12
12
 
13
13
  - if user_signed_in?
14
14
  - if hall.like_user(current_user.id) #ここでエラーが起きています
@@ -59,7 +59,38 @@
59
59
  %span いいね!
60
60
  %span = hall.like_count
61
61
  ```
62
+ ```ruby
63
+ # halls_controller
62
64
 
65
+ class HallsController < ApplicationController
66
+ def index
67
+ @hall = Hall.find_by(id: params[:id])
68
+ end
69
+
70
+ end
71
+ ```
72
+ ```haml
73
+
74
+ # views/halls/index.html.haml
75
+
76
+ .hall
77
+ - if @hall.present?
78
+ - @hall.each do |h|
79
+ .hall__name
80
+ = h.name
81
+ .hall__image
82
+ = image_tag h.image
83
+ .hall__place
84
+ = h.place
85
+ .hall__link
86
+ = link_to top_page_index_path(current_user.id) do
87
+ 詳細をみる
88
+
89
+ .item-button-container
90
+ .item-button-container__left
91
+ = render partial: 'likes/like', locals: { hall: @hall, halls: @halls, likes: @likes, like: @like}
92
+ ```
93
+
63
94
  ### 試したこと
64
95
  def like_user(user_id)
65
96
  likes.find_by(user_id: user_id)