0
votes

This question is related to one that I asked a couple years ago:

Instantiating Devise user models manually using contents of params hash

I am not sure if this is a rails 4 issue, but I am finding that I cannot manually instantiate devise user in my controller code. This used to work in rails 3.

class RegistrationsController < Devise::RegistrationsController

  ...
  def schema_test

    @user = User.new(:email => '[email protected]', :password => 'asdf123', :password_confirmation => 'asdf123')
    @user.save
  end
  ...
end

This is the devise-specific part of my routes.rb: devise_for :users, :controllers => {:registrations => "registrations"}

devise_scope :user do
  get '/schema_test', to: 'registrations#schema_test'
end

When I call schema_test, I am finding that the user object is not getting saved to the database. Does anyone have any suggestions?

1

1 Answers

0
votes

Best practice is to check the behavior in the rails console before you integrate it into your rails application.

The simplest way to figure out your issue is to read the error messages that devise returns. This could be caused by not meeting the password requirements and/or that the email already exists in your database.

From the root of your application, run rails c.

@user = User.new(:email => '[email protected]', :password => 'asdf123', :password_confirmation => 'asdf123')

# Check if the user object is valid
@user.valid?

# If it comes back false, read the error messages
@user.errors.messages

=> {:password=>["is too short (minimum is 8 characters)"]} 

In your example, the user is not being saved because the password is too short.