Rails Tutorial User.save - ruby-on-rails

I am working on Michael Hartl's tutorial (chapter 6). When I try to create a new user in the rails console:
user = User.new(name: "Lord of Darkness", email: "LoD#hell.com")
I get:
=> #<User id: nil, name: "Lord of Darkness", email: "LoD#hell.com",
created_at: nil, updated_at: nil, password_digest: nil>
Which seem to be correct. But when I try to save I get this:
irb(main):007:0> user.save
←[1m←[36m (0.0ms)←[0m ←[1mbegin transaction←[0m
←[1m←[35mUser Exists (0.0ms)←[0m SELECT 1 AS one FROM "users" WHERE LOWER("us
ers"."email") = LOWER('mhartl#example.com') LIMIT 1
←[1m←[36m (0.0ms)←[0m ←[1mrollback transaction←[0m
=> false
which definitely is not what I want. My user_spec is passing all tests.
My user.rb looks like that:
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
What causes this save problem? Without solving this error I cannot go on with the tutorial.

I believe your error is stemming from the fact that the User you are creating doesn't have a password.
Your User model validates a password and requires its presence, but when you create new a User from the command line, he/she doesn't have a password yet.

Related

Model test in Ruby

I've some problems with my model tests in ruby. When I try to use a test of validation, the test produces an error. I created a new model (child model) which has the following validations
class Child < ApplicationRecord
has_many :relations, :dependent => :destroy
accepts_nested_attributes_for :relations
belongs_to :user
validates :user, presence: true
validates :name, presence: true, length: { maximum: 50 }
validates :city, presence: true, :on => :create
validates :postalcode, presence: true, numericality: true
validates :streed, presence: true
validates :add_number, presence: true
validates :disability, presence:true, inclusion: { in: [true, false] }
validates :halal, presence:true, inclusion: { in: [true, false] }
validates :koscha, presence:true, inclusion: { in: [true, false] }
validates :vegetarian, presence:true, inclusion: { in: [true, false] }
validates :vegan, presence:true, inclusion: { in: [true, false] }
validates :allday, presence:true, inclusion: { in: [true, false] }
validates :gender, presence: true
I would like to test my model and wanna use as first test the validation of the child:
def setup
#child = Child.new(name: "Example Child", city: "Example City", postalcode: 13, streed: "Example Street",
add_number: 3, disability: true, gender: 1, halal: true, koscha: false,
vegetarian: false, vegan: false, allday:true, user: "hallo")
end
test "should be valid" do
assert #child.valid?
end
and my fixtures for the children look like this:
one:
name: MyString
city: MyString
postalcode: 1
streed: MyString
add_number: 1
disability: false
halal: false
koscha: false
vegetarian: false
vegan: false
allday: false
gender: 1
user_id: 3
I have the problem, that my validation test produces the following error false to be truth and I can't see, what I've done wrong...
I think, it's a very simple fault...
Thank you for your help!
my validation test produces the following error false to be truth and I can't see, what I've done wrong
This means that #child.valid? returns false. Not what assert expects.
I too don't see what you did wrong. Most likely, some validation failing. It's trivial to find out which one. Just inspect #child.errors. Like this, for example.
test "should be valid" do
is_valid = #child.valid? # trigger validation
p #child.errors unless is_valid
assert is_valid
end
I'm betting on this one:
user: "hallo"
This doesn't look like a valid user object.

Ruby on Rails testing password presence fails

I wanted to test that password should be present for a user signup.
In my test:
def setup
#user = User.new(name: "foobar", email: "foobar#gmail.com",
password: 'password',
password_confirmation: 'password')
end
test "password can't be blank" do
#user.password = nil
assert_not #user.valid?
end
User model:
validates :name, presence: true, length: { maximum: 20 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false}
has_secure_password
validates :password, presence: true, length: { minimum: 8}
this test passes fine, but if I change it to #user.password = "", the test fails. Could anyone help me understand why? what is the difference here for password? For emails, i used "" and it worked fine.
Thanks !
The test for #user.email = "", passes because even you don't specify minimum length validation you have regex validation.
VALID_EMAIL_REGEX =
/\A[\w+-.]+#[a-z\d-]+(.[a-z\d-]+)*.[a-z]+\z/i.
For password, you have minimum length validation, so #user.password = "" should also pass the test.

how to test with sorcery

i have a little problem with sorcery
this is my test :
def setup
#user = users(:anouar)
end
test "should be valid" do
assert #user.valid?
end
and this is my fixture :
anouar:
email: anouar#gmail.com
salt: <%= salt = "asdasdastr4325234324sdfds" %>
crypted_password: <%= Sorcery::CryptoProviders::BCrypt.encrypt("secret", salt) %>
and my model :
class User < ActiveRecord::Base
authenticates_with_sorcery!
validates :email, presence: true, length: { maximum: 255 },
email_format: { message: 'has invalid format' },
uniqueness: { case_sensitive: false }
end
until here when i run bundle exec rake test the test is green
but when i add the validation of the password
validates :password, presence: true, confirmation: true, length: { minimum: 3}
the test "should be valid" is fail
please help?
Did you try to provide directly a password in your fixture ?

NoMethodError: undefined method `save' rails console

I'm learning RoR from ruby.railstutorial.org , i have created a model and when im trying to add data to it and save via rails console im getting error. (Im using mysql )
Rails Console
User.new(username: "test", password: "test123", password_confirmation: "test123", email: "test#fs.in", role: "admin" )
=> #<User id: nil, username: "test", password: "test123", email: "test#fs.in", role: "admin", created_at: "2012-01-01 00:00:00", updated_at: "2012-01-01 00:00:00", password_confirmation: "test123">
User.save gives below error
NoMethodError: undefined method `save' for #<Class:0x0000000422b628> from /home/ramadas/.rvm/gems/ruby-1.9.3-p327#rails3tutorial2ndEd/gems/activerecord-3.2.9/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
from (irb):15
from /home/ramadas/.rvm/gems/ruby-1.9.3-p327#rails3tutorial2ndEd/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/ramadas/.rvm/gems/ruby-1.9.3-p327#rails3tutorial2ndEd/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/ramadas/.rvm/gems/ruby-1.9.3-p327#rails3tutorial2ndEd/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
My Model
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation, :role, :username
validates :username, presence: true , length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX } , uniqueness: { case_sensitive: false }
before_save { |user| user.email = email.downcase }
before_save { |user| user.username = username.downcase }
validates :password, presence: true, length: { minimum: 4 }
validates :password_confirmation, presence: true
end
I have checked for every possible mistake that could happen, but didnt find a solution. please help me out in figuring out and solve this.
Are you calling User.save or User.new(username:...).save?
The class/model User doesn't have a save method defined on it, but instances of the class do (coming from ActiveRecord::Base).
Try the following:
user = User.new(username:...)
user.save

Generate reset password token don't save on model

Hello I trying to create a reset password for my rails app; but when I try to save I get the following error:
Validation failed: Password can't be blank, Password is too short
(minimum is 6 characters), Password confirmation can't be blank
This is my user model.
class User < ActiveRecord::Base
attr_accessible :email, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
VALID_EMAIL_REGEX = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
def send_password_reset
self.password_reset_token = SecureRandom.urlsafe_base64
self.password_reset_at = Time.zone.now
self.password = self.password
self.password_confirmation = self.password
save!
end
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
the method "send_password_reset" doesn't update the user and I don't understand why is trying to save the user instead on updating just password_reset_token and the password_reset_at.
Does anybody can help me, please?
When you call save! on the model instance, it's going to run the validations on your User model; all of them.
There are a number of ways to skip the password validations conditionally. One is to use a Proc
validates :password, presence: true, length: { minimum: 6 }, unless: Proc.new { |a| !a.new_record? && a.password.blank? }
This will allow the User instance to be saved and will skip the validation of the :password field if it's blank and the User is not new (already persisted to the database).
Here is most of a password validation I use in my applications
validates :password, confirmation: true,
length: {:within => 6..40},
format: {:with => /^(?=.*\d)(?=.*([a-z]|[A-Z]))([\x20-\x7E]){6,40}$/},
Notice, you don't need the separate validation on :password_confirmation. Instead just pass confirmation: true to the :password validator.
Suggested Reading:
http://guides.rubyonrails.org/active_record_validations_callbacks.html#confirmation

Resources