Create a Devise User from Ruby Console

Create a devise user from Ruby console

You can add false to the save method to skip the validations if you want.

User.new({:email => "guy@gmail.com", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" }).save(false)

Otherwise I'd do this

User.create!({:email => "guy@gmail.com", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" })

If you have confirmable module enabled for devise, make sure you are setting the confirmed_at value to something like Time.now while creating.

devise user from rails console

UPDATED:
You are missing the {} in your User.new()

user = User.new({email: 'test@example.com', password: 'password', password_confirmation: 'password'})
user.save

Do you have :confirmable option on? If yes, do:

user = User.new({email: 'test@example.com', password: 'password', password_confirmation: 'password'})
user.skip_confirmation!
user.save

How to create user without password and set it later with Devise form?

A different solution altogether would be to use Devise::Invitable that provides the feature that you're probably looking for.

It gives you a /users/invitation/new path with a form that you can fill out which invites users. The user record is saved and then the user completes the registration process by accepting the invitation.

If you really wanted to you could send the invitations from the console with:

User.invite!(email: 'someone@example.com')

But really I would just setup some basic authorization with Pundit or CanCanCan to lock down the invitations controller and do it through the GUI. You're most likely going to need it anyways.

How can I list devise users in a ruby on rails project using the console?

You can run database queries in your Rails console just in the same way as in your controller.

Imaging you have a User model that has an email attribute and the email you want to check are in an emails variable then you can just run the following lines in your Rails console:

emails = %w[foo@example.tld bar@example.tld] 
User.where(email: emails).order(:email).pluck(:email)

Which would return all emails that were found in the database. Or

emails = %w[foo@example.tld bar@example.tld] 
found_emails = User.where(email: emails).order(:email).pluck(:email)
emails - found_emails

Which would return all emails that were not found in the database.

how to add admin with devise in ROR

There's a few things that aren't ideal:

  1. Your attribute User.admin is a boolean, which means without adding a method in User.rb (model), you will be able to do User.admin? and it will return true or false.

  2. It's not a good idea to create your admin user in your method that checks if the current user is an admin. The quickest way is to go into rails console (via your terminal) and updating a User to admin = true, e.g. User.last.update_attribute(:admin, true).

Devise users created in console cannot sign in

I solved this problem. I made a misconfiguration in the devise initializer. I mis-read the config.authentication_keys line and thought that it was either/or for keys and not all of the keys. If another person found this by google, I recommend returning to the auto-generated initializer or git roll back to it. I don't think this had anything to do with the difference in user creation or :registrable module of devise.

How to sign in a user using Devise from a Rails console?

Here's one way I was able to do it:

>> ApplicationController.allow_forgery_protection = false
>> app.post('/sign_in', {"user"=>{"login"=>"login", "password"=>"password"}})

Then you can do:

 >> app.get '/some_other_path_that_only_works_if_logged_in'
>> pp app.response.body

Log out Devise user from console?

Warden stores the user id and the encrypted password in the users cookie and signs it. That means changing their password will sign them out.

Note that if you copy their encrypted password and put it back later, and they still have the cookie, they will be back in as nothing had happened.

If you can't change the password, I'm afraid you can't do it without some black magic (eg. put a piece of code that will only run on their user id and log them out).



Related Topics



Leave a reply



Submit