我无法使用向导表单多步表单将数据图像保存到db 我要实现的目标发生的问题和我收到的错误消息我的编码我的猜测我尝试过的

如何解决我无法使用向导表单多步表单将数据图像保存到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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-