前提・実現したいこと
'devise'を使ってユーザーの新規登録機能を実装しています。
誕生日の入力をdate_selectタグを使って記述し、createアクションを実行したところ以下のようなエラーが表示されます。
エラーを解決し、誕生日の情報をuserテーブルに保存できるようにしたい。
paramsの中身を日付型にする記述をコントローラーないでしていますがうまくいきません。
発生している問題・エラーメッセージ
リンク内容
#####NoMethodError in SignupController#create
######undefined method `[]' for nil:NilClass
ruby
1#signup_controller.rb 2 3 def birthday_join 4 date = params[:user][:birthday] 5 Date.new date["birthday(1i)"].to_i,date["birthday(2i)"].to_i,date["birthday(3i)"].to_i 6 end 7end
該当のソースコード
ruby
1#signup_controller 2 3class SignupController < ApplicationController 4 def index 5 end 6 7 def step1 8 @user = User.new 9 end 10 11 def step2 12 session[:nickname] = user_params[:nickname] 13 session[:email] = user_params[:email] 14 session[:password] = user_params[:password] 15 session[:password_confirmation] = user_params[:password_confirmation] 16 session[:last_name] = user_params[:last_name] 17 session[:first_name] = user_params[:first_name] 18 session[:last_name_kana] = user_params[:last_name_kana] 19 session[:first_name_kana] = user_params[:first_name_kana] 20 session[:birthday] = user_params[:birthday] 21 @user = User.new 22 end 23 24 def create 25 params[:user][:birthday] = birthday_join 26 @user = User.new( 27 nickname: session[:nickname], 28 email: session[:email], 29 password: session[:password], 30 password_confirmation: session[:password_confirmation], 31 last_name: session[:last_name], 32 first_name: session[:first_name], 33 last_name_kana: session[:last_name_kana], 34 first_name_kana: session[:first_name_kana], 35 birthday: session[:birthday], 36 phone_number: session[:phone_number], 37 address_last_name: session[:address_last_name], 38 address_first_name: session[:address_first_name], 39 address_last_name_kana: session[:address_last_name_kana], 40 address_first_name_kana: session[:address_first_name_kana], 41 address_number: session[:address_number], 42 address_prefecture: session[:address_prefecture], 43 address_name: session[:address_name], 44 address_block: session[:address_block], 45 address_building: session[:address_building], 46 address_phone_number: session[:address_phone_number] 47 ) 48 if @user.save 49 session[:id] = @user.id 50 redirect_to done_signup_index_path 51 else 52 render signup_index_path 53 end 54 end 55 56 def done 57 sign_in User.find(session[:id]) unless user_sigend_in? 58 end 59 60 private 61 def user_params 62 params.require(:user).permit( 63 :nickname, 64 :email, 65 :password, 66 :password_confirmation, 67 :last_name, 68 :first_name, 69 :last_name_kana, 70 :first_name_kana, 71 :birthday, 72 :phone_number, 73 :address_last_name, 74 :address_first_name, 75 :address_last_name_kana, 76 :address_first_name_kana, 77 :address_number, 78 :address_prefecture, 79 :address_name, 80 :address_block, 81 :address_building, 82 :address_phone_number 83 ) 84 end 85 86 def birthday_join #paramsの中身を日付型にする記述 87 date = params[:user][:birthday] 88 Date.new date["birthday(1i)"].to_i,date["birthday(2i)"].to_i,date["birthday(3i)"].to_i 89 end 90end
ruby
1#db/migrate/devise_create_users.rb 2 3class DeviseCreateUsers < ActiveRecord::Migration[6.0] 4 def change 5 create_table :users do |t| 6 ## Database authenticatable 7 t.string :nickname, null: false 8 t.string :email, null: false, unique: true 9 t.string :last_name, null: false 10 t.string :first_name, null: false 11 t.string :last_name_kana, null: false 12 t.string :first_name_kana, null: false 13 t.date :birthday, null: false 14 t.integer :phone_number, null: false, unique: true 15 t.string :address_last_name, null: false 16 t.string :address_first_name, null: false 17 t.string :address_last_name_kana, null: false 18 t.string :address_first_name_kana, null: false 19 t.string :address_number, null: false 20 t.integer :address_prefecture, null: false, default: 0 21 t.string :address_name, null: false 22 t.string :address_block, null: false 23 t.string :address_building 24 t.integer :address_phone_number 25 t.string :encrypted_password, null: false, default: "" 26 27 ## Recoverable 28 t.string :reset_password_token 29 t.datetime :reset_password_sent_at 30 31 ## Rememberable 32 t.datetime :remember_created_at 33 t.timestamps null: false 34 end 35 36 add_index :users, :email, unique: true 37 add_index :users, :reset_password_token, unique: true 38 end 39end
haml
1#step2.html.haml 2 3%body 4 %noscript 5 = image_tag 'logo.svg', alt: 'ee' 6 .single-container 7 %header.single-header 8 %h1.header 9 = link_to "#", class:'link' do 10 = image_tag 'logo.svg', alt: 'mercari', class: 'mercari' 11 %nav.progress-bar.singup-bar 12 %main.single-main 13 %section.l-single-container 14 %h2.l-single-head.registration 住所入力 15 = form_for @user, url: '/signup' , method: :post, html: {class: 'l-single-inner'} do |f| 16 %input(type="hidden") 17 .l-single-content 18 .form-group.nickname 19 = f.label :phone_number, "電話番号" 20 %span.form-require 必須 21 = f.number_field :phone_number, placeholder:"例)080-1234-5678", class:"input-default" 22 .form-group.email 23 = f.label :address_number, "郵便番号" 24 %span.form-require 必須 25 = f.number_field :address_number, placeholder:"123-4567", class:"input-default" 26 %input(type="hidden") 27 .form-group.form-password-revelation 28 = f.label :address_prefecture, "都道府県" 29 %span.form-require 必須 30 = f.text_field :address_prefecture, placeholder:"東京都", class:"input-default" 31 %p.form-info-text 32 = f.label :address_name, "住所" 33 %span.form-require 必須 34 = f.text_field :address_name, placeholder:"港区六本木", class:"input-default" 35 %p.form-info-text 36 = f.label :address_block, "番地" 37 %span.form-require 必須 38 = f.text_field :address_block, placeholder:"1-123-4", class:"input-default" 39 .form-password-revelation-taggle 40 .form-password-revelation-revealed-password-container 41 %span.form-password-revelation-revealed-password 42 .form-group.confirmation 43 %h3.text-left 本人確認 44 %p.l-single-text 45 安心・安全にご利用いただくために、お客様の本人情報の登録にご協力ください。他のお客様に公開されることはありません。 46 .form-group.name-kanji 47 %div 48 = f.label "送付先お名前",class:"kanji" 49 %span.form-require 必須 50 = f.text_field :address_last_name, placeholder:"例) 山田", class:"input-default half1" 51 = f.text_field :address_first_name, placeholder:"例) 彩", class:"input-default half2" 52 .form-group.name-kana 53 %div 54 = f.label "送付先お名前カナ(全角)",class:"kana" 55 %span.form-require 必須 56 = f.text_field :address_last_name_kana, placeholder:"例) ヤマダ", class:"input-default half3" 57 = f.text_field :address_first_name_kana, placeholder:"例) アヤ",class:"input-default half4" 58 %input(type="hidden") 59 .clearfit 60 %p.form-info-text 61 ※ 本人情報は正しく入力してください。会員登録後、修正するにはお時間をいただく場合があります。 62 .l-single-content2 63 %input(type="hidden") 64 = f.submit "登録する",class:"submit"
ターミナルの結果は以下のようになります。
リンク内容
"birthday"=>{"birthday(1i)"=>"2019", "birthday(2i)"=>"3", "birthday(3i)"=>"13"}
のように情報が渡っています。
試したこと
・devise_parameter_sanitizerメソッドをapplication.controllerに記述
・require 'date'の記述
補足情報(FW/ツールのバージョンなど)
ruby '2.6.5'
rails '6.0.0'
mysql2 '0.4.4'
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー