前提・実現したいこと
現象:
rails にてユーザー登録機能を
form withで実現させたいが
submitボタンを押しても結果が送信されない
試したこと:
binding.pryを使用し原因を確認
わかったこと:
①
@addressにて permitted: false との結果になっており
その後の処理が上手くできていないため
render :new_address
処理へ切り替わっていることがわかった
②
address_paramsと言うメソッド自体にはデータが登録されており、trueを返している
・・・
ここからどうすれば良いかわからなくなってしまいました。
address_paramsはtrueなのになぜ先へ進まないのだろう
発生している問題・エラーメッセージ
エラーメッセージ 29: def create_address 30: @user = User.new(session["devise.regist_data"]["user"]) 31: @address = Address.new(address_params) 32: if @address.valid? 33: @user.save 34: @address = Address.new(address_params) 35: @address.save 36: session["devise.regist_data"]["user"].clear 37: sign_in(:user, @user) 38: else 39: render :new_address => 40: binding.pry 41: 42: end 43: end [1] pry(#<Users::RegistrationsController>)> @address => #<Address:0x00007fdfa557d3e8 id: nil, user_id: nil, post_number: "225-0003", prefecture: "東京都", city: "渋谷区", address: "道玄坂3-2", apartment: "", created_at: nil, updated_at: nil> [2] pry(#<Users::RegistrationsController>)> @address.save => false [3] pry(#<Users::RegistrationsController>)> address_params => <ActionController::Parameters {"post_number"=>"225-0003", "prefecture"=>"東京都", "city"=>"渋谷区", "address"=>"道玄坂3-2", "apartment"=>"", "user_id"=>nil} permitted: true> [4] pry(#<Users::RegistrationsController>)> params => <ActionController::Parameters {"authenticity_token"=>"MrlmOKsuF0wK4rL9+wtIINHKV+TyhXdzHWstnkURWkw0FCSc1ES3/3Fp4zEZ8WCtBB8sT2vYRbjcDuZbGYtRSg==", "address"=><ActionController::Parameters {"post_number"=>"225-0003", "prefecture"=>"東京都", "city"=>"渋谷区", "address"=>"道玄坂3-2", "apartment"=>""} permitted: false>, "controller"=>"users/registrations", "action"=>"create_address"} permitted: false> [5] pry(#<Users::RegistrationsController>)> params.require(:address) => <ActionController::Parameters {"post_number"=>"225-0003", "prefecture"=>"東京都", "city"=>"渋谷区", "address"=>"道玄坂3-2", "apartment"=>""} permitted: false> [6] pry(#<Users::RegistrationsController>)> params.require(:address).permit(:post_number, :prefecture, :city, :address, :apartment).merge(user_id: @user.id) => <ActionController::Parameters {"post_number"=>"225-0003", "prefecture"=>"東京都", "city"=>"渋谷区", "address"=>"道玄坂3-2", "apartment"=>"", "user_id"=>nil} permitted: true> [7] pry(#<Users::RegistrationsController>)> Started POST "/addresses" for ::1 at 2020-05-07 01:06:02 +0900 [7] pry(#<Users::RegistrationsController>)> exit Completed 200 OK in 5244316ms (Views: 10.4ms | ActiveRecord: 3.5ms | Allocations: 62696)
該当のソースコード
Ruby
1ソースコード 2class Users::RegistrationsController < Devise::RegistrationsController 3 # before_action :configure_sign_up_params, only: [:create] 4 # before_action :configure_account_update_params, only: [:update] 5 6 # GET /resource/sign_up 7 def new 8 @user = User.new 9 end 10 11 # POST /resource 12 def create 13 @user = User.new(sign_up_params) 14 if @user.valid? 15 session["devise.regist_data"] = {user: @user.attributes} 16 session["devise.regist_data"][:user]["password"] = params[:user][:password] 17 redirect_to addresses_path 18 else 19 render :new 20 end 21 end 22 23 def new_address 24 @user = User.new(session["devise.regist_data"]["user"]) 25 @address = Address.new 26 end 27 28 def create_address 29 @user = User.new(session["devise.regist_data"]["user"]) 30 @address = Address.new(address_params) 31 if @address.valid? 32 @user.save 33 @address = Address.new(address_params) 34 @address.save 35 session["devise.regist_data"]["user"].clear 36 sign_in(:user, @user) 37 else 38 render :new_address 39 end 40 end 41 42 protected 43 def address_params 44 params.require(:address).permit(:post_number, :prefecture, :city, :address, :apartment).merge(user_id: @user.id) 45 end 46 47end
環境
ruby 2.7.1
Rails 6.0.2.2
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/10 03:02 編集
2020/05/10 03:06
2020/05/17 00:59