0

我正在使用设计 gem 进行用户会话操作。我想将数据导入管理面板中的用户模型。

红宝石版本:2.4.1p111

Rails 版本:Rails 5.1.4

管理面板 gem:activeadmin

管理面板导入 gem:active_admin_import

管理员/用户.rb

ActiveAdmin.register User do active_admin_import validate: true, template_object: ActiveAdminImport::Model.new( hint: "Dosyanızda veriler belirtilen başlıklar altında olmalıdır: 'email', 'identity_no', 'password', 'password_confirmation'", csv_headers: ['email', 'identity_no', 'password', 'password_confirmation'] ) permit_params :email, :identity_no, :password, :password_confirmation .... ... end

模型/用户.rb

class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_one :profile, dependent: :destroy has_many :graduations, dependent: :destroy has_many :works, dependent: :destroy validates :identity_no, presence: true ... ... end

我收到错误消息: can't write unknown attribute password

如何解决此错误?

4

1 回答 1

3

原因

设计创建encrypted_password数据库字段,而不是password字段,它会覆盖password=进行加密的方法,然后将加密的分配给encrypted_password.

active_admin_import确实直接import,所以没有经过password=method,所以报错

解决方案

用于before_batch_import模拟加密过程并将加密密码分配给encrypted_password字段。不需要password_confirmation。例子:

active_admin_import validate: false,
  before_batch_import: proc { |import|
    import.csv_lines.length.times do |i|
      import.csv_lines[i][2] = User.new(password: import.csv_lines[i][2]).encrypted_password
    end
  },
  template_object: ActiveAdminImport::Model.new(
    csv_headers: ['email', 'identity_no', 'encrypted_password']
  ),
  timestamps: true
于 2017-11-01T15:51:07.607 回答