The answer to your question: User.create returns a User instance if it succeeds or fails. If this fails due to validation, the instance will be invalid and will have errors:
user.valid?
So your code will change:
if user==false or user==nil or user==vals
:
if !user.valid?
You can also use this template:
user.attributes = vals if user.save ... save succeeded ... else ... save failed ... end
The save method returns the boolean true for false since you are calling it on an existing instance.
But let's meet you on the right track in several ways:
First: you have this:
if User.exists(vals[:username])
(I assume that exits is a method that you put in your User model because it is not a Rails thing). Instead of doing this check in your controller, you can simply use a different check on the model:
class User < ActiveRecord::Base ... validates :username, unique: true ... end
Now, when you try to create a user, he will not be able to check if you already have a name with that name.
Second: You have the following:
vals[:create_date] = DateTime.current
It is not necessary. If you add a column to your model called created_at , it will automatically save the creation date (managed by ActiveRecord). You can add this and its updated_at partner to your model in your migration as follows:
create_table :users do |t| ... t.timestamps
Or, since you already have a users table:
add_column :users, :created_at, :datetime add_column :users, :updated_at, :datetime
Now you will always have the creation date / time and the last update of your user model without the need to add additional code.
Third: You have the following:
user = User.create(vals, :without_protection => :true)
Do not do this. Instead, change this:
vals = params[:user]
For this:
vals = params.require(:user).permit(:username, :password, :password_confirmation)
And then save the protection:
user = User.create(vals)
You can add any additional columns that you want to bring from the form to the permit() call. This is very important because it is difficult to resolve later. "If one day you go down the dark path, it will forever dominate your destiny."
Fourth: You cannot redirect the user_path if the failure failed because the user model will not be displayed. Instead, you should redisplay your new form. You also don't need flash error messages. If the new form displays, it can check @user.errors and report errors. See Documentation ActiveRecord Error Documentation .
Finally: You mentioned that your verification failed, even when your password was correctly verified. I canβt say for sure without seeing your form code, but make sure your password field is called password and the confirmation field is password_confirmation . Rails searches for this value of the *_confirmation field specifically when checking for confirmation.
If this is not the case, submit your form code and I will change my mind.