質問編集履歴
2
マークダウン記法に修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,6 +12,12 @@
|
|
12
12
|
|
13
13
|
##エラーメッセージなど
|
14
14
|
|
15
|
+
|
16
|
+
|
17
|
+
```
|
18
|
+
|
19
|
+
|
20
|
+
|
15
21
|
・ビューにはエラ〜メッセージは表示されておりません。
|
16
22
|
|
17
23
|
コントローラーでflaseの時はnewへrenderするようにしているのでfalse時の条件分岐が作用している状態です。
|
@@ -48,6 +54,14 @@
|
|
48
54
|
|
49
55
|
|
50
56
|
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
51
65
|
##やったこと
|
52
66
|
|
53
67
|
①binding.pryで取得データの確認
|
1
マークダウン記法に修正.コントローラー追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -48,7 +48,7 @@
|
|
48
48
|
|
49
49
|
|
50
50
|
|
51
|
-
##
|
51
|
+
##やったこと
|
52
52
|
|
53
53
|
①binding.pryで取得データの確認
|
54
54
|
|
@@ -58,9 +58,11 @@
|
|
58
58
|
|
59
59
|
###検証
|
60
60
|
|
61
|
+
```
|
62
|
+
|
61
63
|
①Post.new(post_params)やpost_paramsにデータが入っていることを確認。
|
62
64
|
|
63
|
-
|
65
|
+
Post.new(post_params)
|
64
66
|
|
65
67
|
User Load (14.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
|
66
68
|
|
@@ -88,9 +90,9 @@
|
|
88
90
|
|
89
91
|
category_id: nil>
|
90
92
|
|
91
|
-
・@post.save!を行うも「NoMethodError: undefined method `save!' for nil:NilClass」
|
93
|
+
・@post.save!を行うも「NoMethodError: undefined method `save!' for nil:NilClass」```
|
92
|
-
|
93
|
-
|
94
|
+
|
95
|
+
|
94
96
|
|
95
97
|
②userとpostのアソシエーションは組めていることを確認
|
96
98
|
|
@@ -106,6 +108,8 @@
|
|
106
108
|
|
107
109
|
has_many :posts
|
108
110
|
|
111
|
+
```
|
112
|
+
|
109
113
|
|
110
114
|
|
111
115
|
###仮説
|
@@ -116,11 +120,23 @@
|
|
116
120
|
|
117
121
|
|
118
122
|
|
119
|
-
|
123
|
+
```
|
120
124
|
|
121
125
|
###コード
|
122
126
|
|
123
|
-
(posts.controller)
|
127
|
+
(posts.controller.rb)
|
128
|
+
|
129
|
+
class PostsController < ApplicationController
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
def index
|
134
|
+
|
135
|
+
@posts = Post.all.limit(3).order("created_at DESC")
|
136
|
+
|
137
|
+
end
|
138
|
+
|
139
|
+
|
124
140
|
|
125
141
|
def new
|
126
142
|
|
@@ -132,7 +148,7 @@
|
|
132
148
|
|
133
149
|
def create
|
134
150
|
|
135
|
-
|
151
|
+
|
136
152
|
|
137
153
|
@post = Post.new(post_params)
|
138
154
|
|
@@ -150,6 +166,30 @@
|
|
150
166
|
|
151
167
|
|
152
168
|
|
169
|
+
# def show
|
170
|
+
|
171
|
+
# end
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
# def destroy
|
176
|
+
|
177
|
+
# end
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
# def edit
|
182
|
+
|
183
|
+
# end
|
184
|
+
|
185
|
+
|
186
|
+
|
187
|
+
# def update
|
188
|
+
|
189
|
+
# end
|
190
|
+
|
191
|
+
|
192
|
+
|
153
193
|
def post_params
|
154
194
|
|
155
195
|
params.require(:post).permit(
|
@@ -157,3 +197,143 @@
|
|
157
197
|
:title, :pharse, :summery, :image).merge(user_id: current_user.id)
|
158
198
|
|
159
199
|
end
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
end
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
(appplication.controller.rb)
|
208
|
+
|
209
|
+
class ApplicationController < ActionController::Base
|
210
|
+
|
211
|
+
before_action :configure_permitted_parameters, if: :devise_controller?
|
212
|
+
|
213
|
+
protect_from_forgery with: :exception
|
214
|
+
|
215
|
+
protected
|
216
|
+
|
217
|
+
def configure_permitted_parameters
|
218
|
+
|
219
|
+
devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :prefecture_id, :city, :birth_year, :birth_month, :birth_day, :age_id, :gender_id])
|
220
|
+
|
221
|
+
end
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
(registrations_controller.rb)
|
228
|
+
|
229
|
+
class Users::RegistrationsController < Devise::RegistrationsController
|
230
|
+
|
231
|
+
before_action :configure_sign_up_params, only: [:create]
|
232
|
+
|
233
|
+
# before_action :configure_account_update_params, only: [:update]
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
def new
|
238
|
+
|
239
|
+
@user = User.new
|
240
|
+
|
241
|
+
end
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
def create
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
@user = User.new(sign_up_params)
|
250
|
+
|
251
|
+
unless @user.valid?
|
252
|
+
|
253
|
+
flash.now[:alert] = @user.errors.full_messages
|
254
|
+
|
255
|
+
render :new and return
|
256
|
+
|
257
|
+
end
|
258
|
+
|
259
|
+
session["devise.regist_data"] = {user: @user.attributes}
|
260
|
+
|
261
|
+
session["devise.regist_data"][:user]["password"] = params[:user][:password]
|
262
|
+
|
263
|
+
@information = @user.build_user_information
|
264
|
+
|
265
|
+
render :new_user_information
|
266
|
+
|
267
|
+
end
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
def create_user_information
|
274
|
+
|
275
|
+
|
276
|
+
|
277
|
+
@user = User.new(session["devise.regist_data"]["user"])
|
278
|
+
|
279
|
+
@information = UserInformation.new(user_information_params)
|
280
|
+
|
281
|
+
unless @information.valid?
|
282
|
+
|
283
|
+
flash.now[:alert] = @information.errors.full_messages
|
284
|
+
|
285
|
+
render :new_user_information and return
|
286
|
+
|
287
|
+
end
|
288
|
+
|
289
|
+
@user.build_user_information(@information.attributes)
|
290
|
+
|
291
|
+
if @user.save!
|
292
|
+
|
293
|
+
sign_in(:user, @user)
|
294
|
+
|
295
|
+
else
|
296
|
+
|
297
|
+
new_user_registration_path
|
298
|
+
|
299
|
+
end
|
300
|
+
|
301
|
+
end
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
protected
|
306
|
+
|
307
|
+
def configure_sign_up_params
|
308
|
+
|
309
|
+
devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
|
310
|
+
|
311
|
+
end
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
|
316
|
+
|
317
|
+
protected
|
318
|
+
|
319
|
+
def user_information_params
|
320
|
+
|
321
|
+
params.require(:user_information).permit(:prefecture_id, :city, :birth_year, :birth_month, :birth_day, :age_id, :gender_id)
|
322
|
+
|
323
|
+
end
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
def after_sign_up_path_for(resource)
|
328
|
+
|
329
|
+
root_path
|
330
|
+
|
331
|
+
end
|
332
|
+
|
333
|
+
end
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
```
|