Rails 3 with Devise for Authentication - How do I manually create a user?

前端 未结 2 1561
猫巷女王i
猫巷女王i 2020-12-24 11:40

I would like to manually create new Users, without forcing them to verify their email address.

The idea is to allow existing users to automatically add

相关标签:
2条回答
  • 2020-12-24 12:13

    The skip_confirmation! method is available to any confirmable model.

    @user = User.new params[:user]
    @user.skip_confirmation! # Sets confirmed_at to Time.now, activating the account
    @user.save
    

    The user account will be activated though. If you don't want that, continue reading.

    Devise uses conditional callbacks to generate the confirmation token and send the email. The callbacks will be called only if confirmation_required? returns true. Redefine it on your model:

    def confirmation_required?
      false
    end
    

    However, this will make the active_for_authentication? method always return true because it takes whether or not confirmation is required into account. We have to redefine that as well:

    def active_for_authentication?
      confirmed? || confirmation_period_valid?
    end
    

    This way, the account will stay inactive and no confirmation email will be sent. You will have to manually activate the user by calling confirm! on the record or just setting confirmed_at to any date.

    It's quite a hack, but it should work.

    For reference: confirmable.rb

    0 讨论(0)
  • 2020-12-24 12:19

    I just want to add for future reference that since Devise 2.2 there is now a skip_confirmation_notification! method available as well which basically does everything from Matheus' post without redefining the methods in the model.

    0 讨论(0)
提交回复
热议问题