如何解决我无法使用向导表单多步表单将数据图像保存到db 我要实现的目标发生的问题和我收到的错误消息我的编码我的猜测我尝试过的
我要实现的目标
我想使用向导表单(多步表单)将用户信息保存到数据库。
我当前正在创建自己的应用程序,并创建了一个向导表单(共3页)以使用设备gem保存用户信息。
第一页:基本信息,例如个人资料图片和姓名 第二页:地址信息(使用自动地址输入功能) 第三页:完成的页面
在我创建其他模型并进行关联并安装了carrierwave gem之前,它一直运行良好。我不确定是什么原因导致的,但是完成上述操作后,我无法再将用户信息保存到数据库中。
发生的问题和我收到的错误消息
首先,我在屏幕上没有收到任何错误消息,但是没有保存数据。 尽管屏幕上没有错误消息,但是在终端中,我得到了如下的ROLLBACK。
(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)
要检查错误,我将.save更改为.save!我在下面收到错误消息。
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,: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
【地址.rb】
class Address < ApplicationRecord
belongs_to :user,optional: true
validates :zipcode,presence: true
mount_uploader :image,ImageUploader
end
我的猜测
我的猜测是,由于错误消息是“图像不能为空白”,因此在第一页上注册的图像未正确传输到第二页。
我尝试过的
尽管我怀疑由于与向导形式之间传输图像有关的问题而发生了错误,但我还是尝试了以下几种方法...
■模型
・关联是否正确?
检查关联是否正确编写。 我检查模型中是否有has_many(one)×belong_to关系。
・外键不能为零吗?
在before_to之后添加“可选:true”
・图像不传送到第二页吗?
在address.rb中也添加“ mount_uploader:image,ImageUploader”
■控制器
・图像不保存吗?
检查是否在application_controller的devise_parameter_sanitizer中编写了“:image”
尽管我一直努力尝试自己解决问题,但我希望收到您的一些建议。 预先谢谢你!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。