質問編集履歴

1

文法の修正

2020/05/28 03:08

投稿

kabigon
kabigon

スコア3

test CHANGED
@@ -1 +1 @@
1
- deviseでデータが保存できない
1
+ date_select生年月日の値を取得できない
test CHANGED
@@ -1,188 +1,40 @@
1
- devise利用て新規登録を実装してのですがデータベース登録されなくて詰まってます。
1
+ 生年月日('年', '月', '日')のような形で取得いのですができずに詰まってます。
2
2
 
3
- 確認たこと・・・デフォルト以外のストロングパラメーターの追加バリデーションの記述ミスApplicationControllerbinding.pry
3
+ か取得できず日1つ1つ試してみると一応取得できるのですが4桁になってしまします。
4
4
 
5
- routes.rbにdevise_for :usersは記載されています
5
+ どなたか分かる方らっしゃいしたらご教授お願いしま、、
6
6
 
7
- コントローラーにはdeviseがルートを作っていると認識しているため追加の記述などはありません。
8
7
 
9
- もし原因わかる方いらっしゃいましたら教えて頂きたいです。よろしくお願いします。
10
8
 
11
- 以下コードです
12
-
13
- ### routes
9
+ ### registrations_controller
14
10
 
15
11
  ```
16
12
 
17
- new_user_session GET /users/sign_in(.:format) devise/sessions#new
13
+ def new
18
14
 
19
- user_session POST /users/sign_in(.:format) devise/sessions#create
15
+ @user = User.new
20
16
 
21
- destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
22
-
23
- new_user_password GET /users/password/new(.:format) devise/passwords#new
24
-
25
- edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
26
-
27
- user_password PATCH /users/password(.:format) devise/passwords#update
28
-
29
- PUT /users/password(.:format) devise/passwords#update
30
-
31
- POST /users/password(.:format) devise/passwords#create
32
-
33
- cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
34
-
35
- new_user_registration GET /users/sign_up(.:format) devise/registrations#new
36
-
37
- edit_user_registration GET /users/edit(.:format) devise/registrations#edit
38
-
39
- user_registration PATCH /users(.:format) devise/registrations#update
40
-
41
- PUT /users(.:format) devise/registrations#update
42
-
43
- DELETE /users(.:format) devise/registrations#destroy
44
-
45
- POST /users(.:format) devise/registrations#create
46
-
47
- ```
17
+ end
48
-
49
- ### hamlファイル
50
-
51
- ```
52
-
53
- .form__content
54
-
55
- = form_with model: @user, url: user_registration_path, id: 'new_user', class: 'new_user', local: true do |f|
56
-
57
- .form__content--title
58
-
59
- 会員情報入力
60
-
61
- .form__input
62
-
63
- .form__input--nickname
64
-
65
- .form__input--title
66
-
67
- ニックネーム
68
-
69
- %span.red 必須
70
-
71
- = f.text_field :nickname, placeholder: "最大6文字", class: "nicknameBox"
72
-
73
- .form__input--email
74
-
75
- .form__title
76
-
77
- メールアドレス
78
-
79
- %span.red 必須
80
-
81
- = f.email_field :email, autofocus: true, placeholder: " メールアドレス", class: "emailBox"
82
-
83
- .form__input--password
84
-
85
- .form__title
86
-
87
- パスワード
88
-
89
- %span.red 必須
90
-
91
- = f.password_field :password, autocomplete: "off",placeholder: "半角英数", class: "passwordBox"
92
-
93
- .form__input--password-confirmation
94
-
95
- .form__title
96
-
97
- 確認用パスワード
98
-
99
- %span.red 必須
100
-
101
- = f.password_field :password_confirmation, autocomplete: "off",class: "confirmation"
102
-
103
- .form__input--name
104
-
105
- .form__title
106
-
107
- お名前(全角)
108
-
109
- %span.red 必須
110
-
111
- %div{style: "display:inline-flex"}
112
-
113
- %form
114
-
115
- = f.text_field :fast_name, placeholder: "例)斎藤", class: "input-name"
116
-
117
- %form
118
-
119
- = f.text_field :last_name, placeholder: "例)太朗", class: "input-name"
120
-
121
- .form__input--name-katakana
122
-
123
- .form__title
124
-
125
- お名前カナ(全角)
126
-
127
- %span.red 必須
128
-
129
- %div{:style => "display:inline-flex"}
130
-
131
- %form
132
-
133
- = f.text_field :fast_name_reading, placeholder: "例)サイトウ", class: "input-name"
134
-
135
- %form
136
-
137
- = f.text_field :last_name_reading, placeholder: "例)タロウ", class: "input-name"
138
-
139
- .form__input--birth-date
140
-
141
- .form__title
142
-
143
- 生年月日
144
-
145
- %span.red 必須
146
-
147
- .form__birth-date
148
-
149
- .year
150
-
151
- =f.date_select(:birth_date, prefix:'birthday', prompt:"--",use_month_numbers:true, start_year:Time.now.year, end_year:1900, date_separator:'/')
152
-
153
- .register-button
154
-
155
- = f.submit "送信", class: "button"
156
-
157
- .explanation
158
18
 
159
19
 
160
20
 
161
- ```
21
+ def create
162
22
 
163
- ### application_controller.rb
23
+ date = params[:birthday]
164
24
 
165
- ```
25
+ params[:user][:birth_date] = birth_date_join(date)
166
26
 
167
- class ApplicationController < ActionController::Base
27
+ binding.pry
168
28
 
29
+ @user = User.new(user_params)
169
30
 
31
+ if @user.save
170
32
 
171
- before_action :basic_auth, if: :production?
33
+ redirect_to edit_view_path
172
34
 
173
- before_action :configure_permitted_parameters, if: :devise_controller?
35
+ else
174
36
 
175
- private
176
-
177
-
178
-
179
- def basic_auth
37
+ render 'new'
180
-
181
- authenticate_or_request_with_http_basic do |username, password|
182
-
183
- username == Rails.application.credentials[:basic_auth][:user] &&
184
-
185
- password == Rails.application.credentials[:basic_auth][:pass]
186
38
 
187
39
  end
188
40
 
@@ -190,216 +42,62 @@
190
42
 
191
43
 
192
44
 
193
- def production?
194
45
 
195
- Rails.env.production?
196
46
 
197
- end
47
+ private
198
48
 
199
-
200
49
 
201
- protected
202
50
 
203
- def configure_permitted_parameters
51
+ def user_params
204
52
 
205
- devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname, :fast_name, :last_name, :fast_name_reading, :last_name_reading, :birth_date])
53
+ params.require(:user).permit(:nickname,:email,:password,:fast_name,:last_name,:fast_name_reading,:last_name_reading,:birth_date)
206
54
 
207
55
  end
208
56
 
209
57
 
210
58
 
59
+ def birth_date_join(date)
60
+
61
+ Date.new date["birth_date(1i)"].to_i,date["birth_date(2i)"].to_i,date["birth_date(3i)"].to_i
62
+
211
- end
63
+ end
212
64
 
213
65
 
214
66
 
215
67
  ```
216
68
 
217
- ### user.rb
69
+ ### コンソール
218
70
 
219
71
  ```
220
72
 
221
- class User < ApplicationRecord
73
+ 66: def create
222
74
 
223
- # Include default devise modules. Others available are:
75
+ 67: date = params[:birthday]
224
76
 
225
- # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
77
+ 68: params[:user][:birth_date] = birth_date_join(date)
226
78
 
227
- devise :database_authenticatable, :registerable,
79
+ => 69: binding.pry
228
80
 
229
- :recoverable, :rememberable, :validatable
81
+ 70: @user = User.new(user_params)
82
+
83
+ 71: if @user.save
84
+
85
+ 72: redirect_to edit_view_path
86
+
87
+ 73: else
88
+
89
+ 74: render 'new'
90
+
91
+ 75: end
92
+
93
+ 76: end
230
94
 
231
95
 
232
96
 
233
- validates :nickname, presence: true, uniqueness: true
97
+ [1] pry(#<Users::RegistrationsController>)> birth_date_join(date)
234
98
 
235
- validates :email, presence: true, uniqueness: true
236
-
237
- validates :encrypted_password, presence: true, length: { minimum: 7 }
238
-
239
- validates :fast_name, presence: true
240
-
241
- validates :last_name, presence: true
242
-
243
- validates :fast_name_reading, presence: true
244
-
245
- validates :last_name_reading, presence: true
246
-
247
- validates :birth_date, presence: true
248
-
249
-
250
-
251
- has_many :items
252
-
253
- has_many :payments
254
-
255
- has_many :addresses
99
+ => Sat, 07 Jul 2018
256
-
257
- end
258
100
 
259
101
 
260
102
 
261
103
  ```
262
-
263
- ### マイグレーションファイル
264
-
265
- ```
266
-
267
-
268
-
269
- class DeviseCreateUsers < ActiveRecord::Migration[5.2]
270
-
271
- def change
272
-
273
- create_table :users do |t|
274
-
275
- ## Database authenticatable
276
-
277
- t.string :nickname, null: false
278
-
279
- t.string :email, null: false, default: ""
280
-
281
- t.string :encrypted_password, null: false, default: ""
282
-
283
- t.string :fast_name, null: false
284
-
285
- t.string :last_name, null: false
286
-
287
- t.string :fast_name_reading, null: false
288
-
289
- t.string :last_name_reading, null: false
290
-
291
- t.integer :birth_date, null: false
292
-
293
-
294
-
295
- ## Recoverable
296
-
297
- t.string :reset_password_token
298
-
299
- t.datetime :reset_password_sent_at
300
-
301
-
302
-
303
- ## Rememberable
304
-
305
- t.datetime :remember_created_at
306
-
307
-
308
-
309
-
310
-
311
- t.timestamps null: false
312
-
313
- end
314
-
315
-
316
-
317
- add_index :users, [:nickname, :email], unique: true
318
-
319
- add_index :users, :reset_password_token, unique: true
320
-
321
- # add_index :users, :confirmation_token, unique: true
322
-
323
- # add_index :users, :unlock_token, unique: true
324
-
325
- end
326
-
327
- end
328
-
329
-
330
-
331
- ``` application_controller.rbでbinding.pryをかけてみました
332
-
333
- ```
334
-
335
- From: /Users/ryonukiyama/projects/freemarket_sample_75b/app/controllers/application_controller.rb:4 :
336
-
337
-
338
-
339
- 1: class ApplicationController < ActionController::Base
340
-
341
- 2: before_action :basic_auth, if: :production?
342
-
343
- 3: before_action :configure_permitted_parameters, if: :devise_controller?
344
-
345
- => 4: binding.pry
346
-
347
- 5: private
348
-
349
- 6:
350
-
351
- 7: def basic_auth
352
-
353
- 8: authenticate_or_request_with_http_basic do |username, password|
354
-
355
- 9: username == Rails.application.credentials[:basic_auth][:user] &&
356
-
357
-
358
-
359
- [1] pry(ApplicationController)> @user
360
-
361
- => nil
362
-
363
- [2] pry(ApplicationController)> params
364
-
365
- NameError: undefined local variable or method `params' for ApplicationController:Class
366
-
367
- from (pry):2:in `<class:ApplicationController>'
368
-
369
- [3] pry(ApplicationController)> exit
370
-
371
- Processing by Devise::RegistrationsController#create as HTML
372
-
373
- Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZQrLtyqaKqv/6BgevnMby/ntVwQ7GIezzecCv6PmZBbMD4FfdXhhf8dgpZvCLOfkQmdH2oVuIA9w/7b0sdnWSQ==", "user"=>{"nickname"=>"あいうえお", "email"=>"aaaaa@aaa", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "fast_name"=>"森"}, "birthday"=>{"birth_date(1i)"=>"2011", "birth_date(2i)"=>"7", "birth_date(3i)"=>"10"}, "commit"=>"送信"}
374
-
375
- (0.2ms) BEGIN
376
-
377
- ↳ /Users/ryonukiyama/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/log_subscriber.rb:98
378
-
379
- User Exists (0.3ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'aaaaa@aaa' LIMIT 1
380
-
381
- ↳ /Users/ryonukiyama/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/log_subscriber.rb:98
382
-
383
- User Exists (0.2ms) SELECT 1 AS one FROM `users` WHERE `users`.`nickname` = BINARY 'あいうえお' LIMIT 1
384
-
385
- ↳ /Users/ryonukiyama/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/log_subscriber.rb:98
386
-
387
- CACHE User Exists (0.0ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'aaaaa@aaa' LIMIT 1
388
-
389
- ↳ /Users/ryonukiyama/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/log_subscriber.rb:98
390
-
391
- (0.3ms) ROLLBACK
392
-
393
- ↳ /Users/ryonukiyama/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/activerecord-5.2.4.3/lib/active_record/log_subscriber.rb:98
394
-
395
- Rendering devise/registrations/new.html.haml within layouts/application
396
-
397
- Rendered view/_header.html.haml (5.5ms)
398
-
399
- Rendered view/_footer.html.haml (4.6ms)
400
-
401
- Rendered devise/registrations/new.html.haml within layouts/application (32.5ms)
402
-
403
- Completed 200 OK in 253ms (Views: 92.5ms | ActiveRecord: 18.2ms)
404
-
405
- ```