Reputation: 21
I would like to save user information to db using a wizard form(multistep form).
I am currently creating my own app and I created a wizard form(3 pages) to save user information using device gem.
First page: basic info such as a profile picture and name Second page: address info(using Automatic address input function) Third page: completed page
It was working fine until I created other models and did association and also installing carrierwave gem. I am not sure what led to this but after doing things above I could not save user info to the db anymore.
Firstly, I did not get any error messages on the screen but data was not saved. Although there was no error messages on the screen, in the terminal, I got ROLLBACK as below.
(0.2ms) BEGIN
User Exists (0.3ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'aa@aa' LIMIT 1
(0.2ms) ROLLBACK
Completed 422 Unprocessable Entity in 117ms (ActiveRecord: 0.7ms)
To check the error, I changed .save to .save! and I got error message below.
ActiveRecord::RecordInvalid in Users::RegistrationsController#create_address
Validation failed: Image can't be blank
【registrations_Controller.rb】
# frozen_string_literal: true
class Users::RegistrationsController < Devise::RegistrationsController
# before_action :configure_sign_up_params, only: [:create]
# before_action :configure_account_update_params, only: [:update]
def new
@user = User.new
end
def create
@user = User.new(sign_up_params)
unless @user.valid?
flash.now[:alert] = @user.errors.full_messages
render :new and return
end
session["devise.regist_data"] = {user: @user.attributes}
session["devise.regist_data"][:user]["password"] = params[:user][:password]
@address = @user.build_address
render :new_address
end
def create_address
@user = User.new(session["devise.regist_data"]["user"])
@address = Address.new(address_params)
unless @address.valid?
flash.now[:alert] = @address.errors.full_messages
render :new_address and return
end
@user.build_address(@address.attributes)
@user.save!
session["devise.regist_data"]["user"].clear
sign_in(:user, @user)
end
# GET /resource/edit
# def edit
# super
# end
# PUT /resource
# def update
# super
# end
# DELETE /resource
# def destroy
# super
# end
# GET /resource/cancel
# Forces the session data which is usually expired after sign
# in to be expired now. This is useful if the user wants to
# cancel oauth signing in/up in the middle of the process,
# removing all OAuth session data.
# def cancel
# super
# end
# protected
# If you have extra params to permit, append them to the sanitizer.
# def configure_sign_up_params
# devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute])
# end
# If you have extra params to permit, append them to the sanitizer.
# def configure_account_update_params
# devise_parameter_sanitizer.permit(:account_update, keys: [:attribute])
# end
# The path used after sign up.
# def after_sign_up_path_for(resource)
# super(resource)
# end
# The path used after sign up for inactive accounts.
# def after_inactive_sign_up_path_for(resource)
# super(resource)
# end
protected
def address_params
params.require(:address).permit(:zipcode, :prefecture_code, :city,:district, :building, :room)
end
end
【Application_controller.rb】
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
def after_sign_in_path_for(resource)
posts_path
end
def after_sign_out_path_for(resource)
root_path
end
protected
def configure_permitted_parameters
devise_parameter_sanitizer.permit(:sign_up, keys: [:nickname,:first_name,:last_name, :first_name_kana, :last_name_kana,:birthday,:image])
end
end
【user.rb】
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
validates :nickname,:first_name,:last_name, :first_name_kana, :last_name_kana,:birthday,:image ,presence: true
has_one :address
has_many :posts
has_many :messages
has_many :group_users, dependent: :destroy
has_many :groups, through: :group_users, dependent: :destroy
include JpPrefecture
jp_prefecture :prefecture_code
mount_uploader :image, ImageUploader
def prefecture_name
JpPrefecture::Prefecture.find(code: prefecture_code).try(:name)
end
def prefecture_name=(prefecture_name)
self.prefecture_code = JpPrefecture::Prefecture.find(name: prefecture_name).code
end
end
【address.rb】
class Address < ApplicationRecord
belongs_to :user, optional: true
validates :zipcode, :prefecture_code, :city,:district,presence: true
mount_uploader :image, ImageUploader
end
My guess is that the since the error messages is "image can't be blank", the image that was registered at the first page is not correctly transferred to the second page.
Despite the fact that I suspect that the error is occurred due to the issue related to transferring image among wizard form, I tried several things below...
■model
・association is not done correctly ?
Checked if the association is written correctly. I check if there are has_many(one)×belong_to relationship written in the model.
・Not allowed nil for external key?
add "optional: true" after belong_to
・image is not transferred to the second page?
add "mount_uploader :image, ImageUploader" in the address.rb as well
■Controller
・Image is not saved?
Checked if there is ":image" written in devise_parameter_sanitizer of application_controller
Although I keep try solving by myself, I look forward to hearing some advice from you. Thank you in advance!
Upvotes: 1
Views: 36