質問編集履歴
1
内容の改善
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
Ruby on Railsでユーザーログイン後のコメント
|
1
|
+
Ruby on Railsでユーザーログイン後のコメント保存ができない
|
test
CHANGED
@@ -1,106 +1,60 @@
|
|
1
|
-
Ruby
|
1
|
+
RubyonRailsでユーザーログインしている方のみがコメントをできる仕組みを作っています。
|
2
2
|
|
3
|
-
|
3
|
+
投稿まではできている(ように見える)のですが、実際に保存がされません。
|
4
4
|
|
5
|
-
|
5
|
+
(エラーメッセージは出ず、すました感じでホーム画面に戻ります汗)
|
6
|
+
|
7
|
+
チュートリアル通りに進めているはずなのですが・・・
|
6
8
|
|
7
9
|
|
8
10
|
|
9
|
-
|
11
|
+
すみませんが、教えていただけませんでしょうか?
|
10
|
-
|
11
|
-
undefined method `logged_in?' for #CommentsController
|
12
12
|
|
13
13
|
|
14
14
|
|
15
|
-
|
15
|
+
環境:
|
16
16
|
|
17
|
-
|
17
|
+
Ruby on Rails 5.0.0.1
|
18
18
|
|
19
|
-
|
19
|
+
Devise
|
20
|
-
|
21
|
-
flash[:danger] = "ログインしてください"
|
22
|
-
|
23
|
-
redirect_to login_url
|
24
|
-
|
25
|
-
```
|
26
|
-
|
27
|
-
上記のエラーが出てしまいます。
|
28
20
|
|
29
21
|
|
30
22
|
|
31
|
-
|
23
|
+
##該当するソースコード
|
32
24
|
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
comment.rb
|
36
26
|
|
37
27
|
```ruby
|
38
28
|
|
39
|
-
|
29
|
+
class Comment < ApplicationRecord
|
40
30
|
|
41
|
-
|
31
|
+
belongs_to :user
|
42
32
|
|
43
|
-
|
33
|
+
belongs_to :article
|
44
34
|
|
45
|
-
|
35
|
+
default_scope -> { order(created_at: :desc)}
|
46
36
|
|
47
|
-
en
|
37
|
+
validates :user_id, presence: true
|
48
38
|
|
39
|
+
# validates :article_id, presence: true
|
49
40
|
|
50
|
-
|
51
|
-
|
41
|
+
validates :content, presence: true, length: { maximum: 2000, minimum: 100 }
|
52
|
-
|
53
|
-
def logged_in?
|
54
|
-
|
55
|
-
!current_user.nil?
|
56
|
-
|
57
|
-
end
|
58
42
|
|
59
43
|
end
|
60
44
|
|
61
45
|
```
|
62
46
|
|
63
|
-
▼
|
47
|
+
▼ comment_controller.rb
|
64
48
|
|
65
49
|
```ruby
|
66
50
|
|
67
|
-
|
51
|
+
class commentsController < ApplicationController
|
68
52
|
|
69
|
-
private
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
def logged_in_user
|
74
|
-
|
75
|
-
unless logged_in?
|
76
|
-
|
77
|
-
store_location
|
78
|
-
|
79
|
-
flash[:danger] = "ログインしてください"
|
80
|
-
|
81
|
-
redirect_to login_url
|
82
|
-
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|
88
|
-
|
89
|
-
```
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
▼ comments_controller
|
94
|
-
|
95
|
-
```ruby
|
96
|
-
|
97
|
-
class CommentsController < ApplicationController
|
98
|
-
|
99
|
-
before_action :
|
53
|
+
before_action :signed_in_user
|
100
54
|
|
101
55
|
before_action :correct_user, only: :destroy
|
102
56
|
|
103
|
-
|
57
|
+
|
104
58
|
|
105
59
|
def create
|
106
60
|
|
@@ -120,23 +74,23 @@
|
|
120
74
|
|
121
75
|
end
|
122
76
|
|
123
|
-
|
77
|
+
|
124
78
|
|
125
79
|
def show
|
126
80
|
|
127
|
-
@comment =
|
81
|
+
@comment = comment.find(params[:id])
|
128
82
|
|
129
83
|
end
|
130
84
|
|
131
|
-
|
85
|
+
|
132
86
|
|
133
87
|
def new
|
134
88
|
|
135
|
-
@comment = current_user.comments.build if
|
89
|
+
@comment = current_user.comments.build if signed_in?
|
136
90
|
|
137
91
|
end
|
138
92
|
|
139
|
-
|
93
|
+
|
140
94
|
|
141
95
|
def destroy
|
142
96
|
|
@@ -148,31 +102,31 @@
|
|
148
102
|
|
149
103
|
end
|
150
104
|
|
151
|
-
|
105
|
+
|
152
106
|
|
153
107
|
def edit
|
154
108
|
|
155
109
|
end
|
156
110
|
|
157
|
-
|
111
|
+
|
158
112
|
|
159
113
|
def update
|
160
114
|
|
161
115
|
end
|
162
116
|
|
163
|
-
|
117
|
+
|
164
118
|
|
165
119
|
private
|
166
120
|
|
167
|
-
|
121
|
+
|
168
122
|
|
169
123
|
def comment_params
|
170
124
|
|
171
|
-
params.require(:comment).permit(:id,:content, :
|
125
|
+
params.require(:comment).permit(:id,:content, :title, :article_id, :user_id)
|
172
126
|
|
173
127
|
end
|
174
128
|
|
175
|
-
|
129
|
+
|
176
130
|
|
177
131
|
def correct_user
|
178
132
|
|
@@ -184,6 +138,56 @@
|
|
184
138
|
|
185
139
|
end
|
186
140
|
|
141
|
+
```
|
142
|
+
|
143
|
+
▼ マイグレーションファイル
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
|
147
|
+
class CreateComments < ActiveRecord::Migration[5.0]
|
148
|
+
|
149
|
+
def change
|
150
|
+
|
151
|
+
create_table :comments do |t|
|
152
|
+
|
153
|
+
t.text :title
|
154
|
+
|
155
|
+
t.text :content
|
156
|
+
|
157
|
+
t.references :user, foreign_key: true
|
158
|
+
|
159
|
+
t.references :article, foreign_key: true
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
t.timestamps
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
add_index :comments, [:user_id, :article_id, :created_at]
|
168
|
+
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
172
|
+
|
187
173
|
|
188
174
|
|
189
175
|
```
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
###試してみたこと
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
modelにparamsを追加してみたのですが、やはり上手くいきませんでした。。
|
184
|
+
|
185
|
+
```ruby
|
186
|
+
|
187
|
+
def comment_params
|
188
|
+
|
189
|
+
params.require(:comment).permit(:id,:content, :title, :article_id, :user_id)
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
```
|