Wizard-style new membership does not save information to db

Asked 2 years ago, Updated 2 years ago, 76 views

Prerequisites/What you want to achieve

I would like to register my user information in db by registering as a new member of the wizard method.

We are currently creating a personal development application.
We are trying to achieve user registration using the wizard form using the device.
Page 1: Profile image, name, etc.
Page 2: Address (including automatic address entry
) Page 3: Completion Page

Before I created another model, I was able to register without any problems, but
After the creation and association implementation of other models, it is no longer saved in db.
(We also install carrierwave, so it's not clear where it's no longer possible.)

Problems/Error Messages you are experiencing

There were no specific error statements, so when I changed from save to save!, I received the following error statement:

Enter a description of the image here
Also, the console is ROLLBACK as shown below.

Enter a description of the image here

Source Codes Affected

[registrations_Controller.rb]
Device controller for wizard-driven forms.

#frozen_string_literal:true

class Users::RegistrationsController <Device::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)
    [email protected] lid?
      flash.now [:alert] = @user.errors.full_messages
      render:new and return
    end
    session["device.register_data"] = {user:@user.attributes}
    session["device.register_data"][:user]["password"]=params[:user][:password]
    @[email protected]_address
    render —new_address
  end
  
  def create_address
    @user=User.new(session["device.register_data"]["user"])
    @address=Address.new(address_params)

    [email protected] lid?
      flash.now [:alert] = @address.errors.full_messages
      render —new_address and return
    end
    @user.build_address(@address.attributes)
    @user.save!
    session["device.register_data"]["user"].clear
    sign_in(:user,@user)
  end
  # GET/resource/edit
  # default
  #   super
  # end

  # PUT/resource
  # default update
  #   super
  # end

  # DELETE/resource
  # def destroy
  #   super
  # end

  # GET/resource/cancel
  # Forces the session data which is userally 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
  #   device_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
  #   device_parameter_sanitizer.permit(:account_update, keys:[:attribute])
  # end

  # The path used after sign up.
  # after_sign_up_path_for (resource)
  #   super(resource)
  # end

  # The path used after sign up for inactive accounts.
  # after_inactive_sign_up_path_for (resource)
  #   super(resource)
  # end
  protected

  default address_params
    params.require(:address).permit(:zipcode,:prefect_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: —device_controller?
  after_sign_in_path_for (resource)
    posts_path
  end
  after_sign_out_path_for (resource)
    root_path
  end
  protected

  def configure_permitted_parameters
    device_parameter_sanitizer.permit(:sign_up, keys:[:nickname,:first_name,:last_name,:first_name,:first_name_kana,:last_name_kana,:birthday,:image])
  end
end

[user.rb]

class User<ApplicationRecord
  # Include default device modules.Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  device:database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable
  values: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

  defect_name
    JpPrefecture::Prefecture.find(code:prefecture_code).try(:name)
  end

  def perfect_name = (prefect_name)
    self.prefecture_code = JpPrefecture::Prefecture.find(name:prefecture_name).code
  end

end

[address.rb]

class Address <ApplicationRecord
  belongs_to:user, optional:true
  values:zipcode,:prefecture_code,:city,:distribute,presence:true
  mount_uploader: image, ImageUploader
end

Tried

■ Model
·Is the association not correct?

Other models associated with the user model are address, post, group, and message.
I checked if the relationship between has_many(one) and Belong to is well described in each

·Nil for foreign key not allowed?
Belong_to followed by optional:true

·Is the image not correctly taken over to the second page (address registration)?
I don't know if it's correct, but add mount_uploader: image, ImageUploader to address.rb

■ Controller ·Is the image not saved because Validation failed: Image can't be blank?
Verify that the image is listed in the device_parameter_sanitizer of the application_controller

·Add に@address.user=current_user under @address=Address.new(address_params)
It was a question that was asked elsewhere, and it was an answer, so I also tried it.

This is my first time using stack overflow.
I would appreciate it if you could let me know if there is anything difficult to understand how to ask.
Thank you for your cooperation.

ruby-on-rails ruby database

2022-09-30 11:49

1 Answers

image has failed, so it appears that the image uploaded in carrierwave and not yet saved has not been successfully taken over via session.

Since image_cache is required as well as image_cache to handle images that are not saved across HTTP requests in carrierwave, shouldn't @user.image_cache also be saved in session? (I've only used image_cache to set hidden in validation error, and there was no immediate environment, so it may not be @user.image_cache.)

register_data is better than register_data although it is not directly related to the error.
https://labs.cybozu.co.jp/blog/akky/2005/07/regist/


2022-09-30 11:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.