発生している問題・エラーメッセージ
某プログラミングスクールでフリマアプリを作っています。現在Deviseを使いつつ、ウィザード形式での個人情報登録(氏名や住所など)を実装しています。しかし、フォーム入力後にデータベースに情報が保存されないという状況です。。
発生している問題・エラーメッセージ
特にエラー文はありません。
該当のソースコード
/app/controllers/application_controller.rb
Ruby
1class ApplicationController < ActionController::Base 2 before_action :basic_auth, if: :production? 3 before_action :configure_permitted_parameters, if: :devise_controller? 4 5 protected 6 def configure_permitted_parameters 7 devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname]) 8 devise_parameter_sanitizer.permit(:sign_up, keys: [:last_name]) 9 devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name]) 10 devise_parameter_sanitizer.permit(:sign_up, keys: [:last_name_kana]) 11 devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name_kana]) 12 devise_parameter_sanitizer.permit(:sign_up, keys: [:birth_year]) 13 devise_parameter_sanitizer.permit(:sign_up, keys: [:birth_month]) 14 devise_parameter_sanitizer.permit(:sign_up, keys: [:birth_date]) 15 devise_parameter_sanitizer.permit(:sign_up, keys: [:phone_number]) 16 end 17 protect_from_forgery with: :exception 18 19 private 20 21 def production? 22 Rails.env.production? 23 end 24 25 def basic_auth 26 authenticate_or_request_with_http_basic do |username, password| 27 username == ENV["BASIC_AUTH_USER"] && password == ENV["BASIC_AUTH_PASSWORD"] 28 end 29 end 30end
/app/controllers/users/registrations_controller.rb
Ruby
1# frozen_string_literal: true 2 3class Users::RegistrationsController < Devise::RegistrationsController 4 # before_action :configure_sign_up_params, only: [:create] 5 # before_action :configure_account_update_params, only: [:update] 6 7 # GET /resource/sign_up 8 def new 9 @user = User.new 10 end 11 12 # POST /resource 13 def create 14 @user = User.new(sign_up_params) 15 unless @user.valid? 16 flash.now[:alert] = @user.errors.full_messages 17 render :new and return 18 end 19 session["devise.regist_data"] = {user: @user.attributes} 20 session["devise.regist_data"][:user]["password"] = params[:user][:password] 21 @address = @user.address.build 22 render :new_address 23 end 24 25 def create_address 26 @user = User.new(session["devise.regist_data"]["user"]) 27 @address = Address.new(address_params) 28 unless @address.valid? 29 flash.now[:alert] = @address.errors.full_messages 30 render :new_address and return 31 end 32 @user.address.build(@address.attributes) 33 @user.save 34 sign_in(:user, @user) 35 end 36 37 # GET /resource/edit 38 # def edit 39 # super 40 # end 41 42 # PUT /resource 43 # def update 44 # super 45 # end 46 47 # DELETE /resource 48 # def destroy 49 # super 50 # end 51 52 # GET /resource/cancel 53 # Forces the session data which is usually expired after sign 54 # in to be expired now. This is useful if the user wants to 55 # cancel oauth signing in/up in the middle of the process, 56 # removing all OAuth session data. 57 # def cancel 58 # super 59 # end 60 61 # protected 62 63 # If you have extra params to permit, append them to the sanitizer. 64 # def configure_sign_up_params 65 # devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) 66 # end 67 68 # If you have extra params to permit, append them to the sanitizer. 69 # def configure_account_update_params 70 # devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 71 # end 72 73 # The path used after sign up. 74 # def after_sign_up_path_for(resource) 75 # super(resource) 76 # end 77 78 # The path used after sign up for inactive accounts. 79 # def after_inactive_sign_up_path_for(resource) 80 # super(resource) 81 # end 82 83 84 protected 85 86 def configure_sign_up_params 87 devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) 88 end 89 90 def address_params 91 params.require(:address).permit(:post_code, :prefecture, :city, :block, :building, :last_name, :first_name, :last_name_kana, :first_name_kana, :phone_number) 92 end 93end 94
app/views/devise/registrations/new.html.haml
haml
1.user-registration 2 %h2 ユーザー情報登録 3 = form_for(@user, url: user_registration_path) do |f| 4 = render "devise/shared/error_messages", resource: @user 5 .field 6 = f.label :ニックネーム 7 %br/ 8 = f.text_field :nickname, autofocus: true, maxlength: "10" 9 .field 10 = f.label :姓 11 %br/ 12 = f.text_field :last_name, autofocus: true, maxlength: "5" 13 .field 14 = f.label :名 15 %br/ 16 = f.text_field :first_name, autofocus: true, maxlength: "5" 17 .field 18 = f.label :カナ姓 19 %br/ 20 = f.text_field :last_name_kana, autofocus: true, maxlength: "20" 21 .field 22 = f.label :カナ名 23 %br/ 24 = f.text_field :first_name_kana, autofocus: true, maxlength: "20" 25 .field 26 = f.label :生年月日 27 %br/ 28 = f.text_field :birth_year, autofocus: true, maxlength: "4" 29 = f.text_field :birth_month, autofocus: true, maxlength: "2" 30 = f.text_field :birth_date, autofocus: true, maxlength: "2" 31 .field 32 = f.label :電話番号 33 %br/ 34 = f.text_field :phone_number, autofocus: true, maxlength: "12" 35 .field 36 = f.label :Eメール 37 %br/ 38 = f.email_field :email, autofocus: true, autocomplete: "email" 39 .field 40 = f.label :パスワード 41 %br/ 42 = f.password_field :password, autocomplete: "new-password", minimumlength: "7" 43 .field 44 = f.label :パスワード再入力 45 %br/ 46 = f.password_field :password_confirmation, autocomplete: "new-password", minimumlength: "7" 47 48 .actions 49 = f.submit "Next" 50 = render "devise/shared/links" 51
app/views/devise/registrations/new_address.html.haml
Haml
1%h2 住所情報登録 2= form_for @address do |f| 3 = render "devise/shared/error_messages", resource: @address 4 .field 5 = f.label :郵便番号 6 %br/ 7 = f.text_field :post_code, autofocus: true, maxlength: "7" 8 .field 9 = f.label :都道府県 10 %br/ 11 = f.text_field :prefecture, autofocus: true 12 .field 13 = f.label :市区町村 14 %br/ 15 = f.text_field :city, autofocus: true 16 .field 17 = f.label :番地 18 %br/ 19 = f.text_field :block, autofocus: true 20 .field 21 = f.label :マンション名 22 %br/ 23 = f.text_field :building, autofocus: true 24 %h1 送付先氏名 25 .field 26 = f.label :姓 27 %br/ 28 = f.text_field :last_name, autofocus: true 29 .field 30 = f.label :名 31 %br/ 32 = f.text_field :first_name, autofocus: true 33 .field 34 = f.label :カナ姓 35 %br/ 36 = f.text_field :last_name_kana, autofocus: true 37 .field 38 = f.label :カナ名 39 %br/ 40 = f.text_field :first_name_kana, autofocus: true 41 .field 42 = f.label :電話番号 43 %br/ 44 = f.text_field :phone_number, autofocus: true 45 .actions 46 = f.submit "Sign up" 47= render "devise/shared/links"
db/schema.rb
Ruby
1ActiveRecord::Schema.define(version: 2020_02_01_064100) do 2 3 create_table "addresses", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 4 t.integer "post_code", null: false 5 t.string "prefecture", null: false 6 t.string "city", null: false 7 t.string "block", null: false 8 t.string "building", null: false 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.string "phone_number", null: false 14 t.bigint "user_id" 15 t.datetime "created_at", null: false 16 t.datetime "updated_at", null: false 17 t.index ["user_id"], name: "index_addresses_on_user_id" 18 end 19 20 create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t| 21 t.string "email", default: "", null: false 22 t.string "encrypted_password", default: "", null: false 23 t.string "reset_password_token" 24 t.datetime "reset_password_sent_at" 25 t.datetime "remember_created_at" 26 t.datetime "created_at", null: false 27 t.datetime "updated_at", null: false 28 t.string "nickname", default: "", null: false 29 t.string "last_name", default: "", null: false 30 t.string "first_name", default: "", null: false 31 t.integer "birth_year", null: false 32 t.integer "birth_month", null: false 33 t.integer "birth_date", null: false 34 t.string "phone_number", default: "", null: false 35 t.string "last_name_kana" 36 t.string "first_name_kana", null: false 37 t.index ["email"], name: "index_users_on_email", unique: true 38 t.index ["nickname"], name: "index_users_on_nickname" 39 t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true 40 end 41end 42
あなたの回答
tips
プレビュー