Devise: Create users without password

前端 未结 3 1509
难免孤独
难免孤独 2021-01-03 20:23

In our application we have normal users. However, we want to be able to make invitations, to invite certain people. Note that an invitation is directly coupled to a user, as

3条回答
  •  误落风尘
    2021-01-03 20:25

    There are at least two ways to do what you want:

    Method 1:

    Overload Devise's password_required? method

    class User < ActiveRecord::Base
      attr_accessor :skip_password_validation  # virtual attribute to skip password validation while saving
    
      protected
    
      def password_required?
        return false if skip_password_validation
        super
      end
    end
    

    Usage:

    @user.skip_password_validation = true
    @user.save
    

    Method 2:

    Disable validation with validate: false option:

    user.save(validate: false)
    

    This will skip validation of all fields (not only password). In this case you should make sure that all other fields are valid.

    ...

    But I advise you to not create users without password in your particular case. I would create some additional table (for example, invitations) and store all required information including the fields that you want to be assigned to a user after confirmation.

提交回复
热议问题