0

I am using devise gem for user session operations. I want to import data to the user model in the admin panel.

Ruby version: 2.4.1p111

Rails version: Rails 5.1.4

Admin panel gem: activeadmin

Admin panel import gem: active_admin_import

admin/user.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

models/user.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

I received the error message: can't write unknown attribute password

How can I resolve this error?

Fivell
  • 11,829
  • 3
  • 61
  • 99

1 Answers1

3

Reason

Devise creates encrypted_password database field, not password field and it overrides password= method to do the encryption then assigns encrypted one to encrypted_password.

active_admin_import does directly import, so it doesn't go through password= method, so the error occurs

Solution

Use before_batch_import to simulate encrypting process and assign encrypted password to encrypted_password field. No password_confirmation needed. Example:

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
Nam Tran
  • 418
  • 3
  • 6