NoMethodError: undefined method `save' rails console - ruby-on-rails

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

Related

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 ?

Ruby on Rails 4.2.0 with rspec-rails 3.1.0 and shoulda-matchers 2.7.0 undefined method `validates_presence_of' for

i'm using Ruby on Rails 4.2.0 with rspec-rails 3.1.0 and shoulda-matchers 2.7.0
when run test i have this error
Failure/Error: it { should validates_presence_of :name }
NoMethodError:
undefined method `validates_presence_of' for #<RSpec::ExampleGroups::Profile::Vaidations:0x00000007881768>
# ./spec/models/profile_spec.rb:5:in `block (3 levels) in <top (required)>'
this is my model
class Profile < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
validates :user, presence: true
validates :username, presence: true, uniqueness: { case_sensitive: false }, length: {maximum: 50}
validates :name, presence: true, length: {maximum: 50}
validates :phone, presence: true, uniqueness: { case_sensitive: false }, length: {maximum: 50}
validates :age, presence: true, length: {maximum: 4}
end
and this is my spec file
require 'rails_helper'
describe Profile do
describe "vaidations" do
it { should validates_presence_of :name }
it { should validates_presence_of :age }
it { should validates_numericality_of :age }
it { should validates_presence_of :phone }
it { should validates_uniqueness_of :phone }
it { should validates_presence_of :username }
it { should validates_uniqueness_of :username }
end
describe 'association' do
it { should belongs_to :user }
end
end
and this is my test group in my gemfile
group :test do
gem 'rspec-rails'
gem 'shoulda-matchers', require: false
gem 'factory_girl_rails'
end
i find the same error on shoulda-matchers repo but with different versions and it didn't work for me!!
what can i do?!!
Please replace following code.
it { should validates_presence_of :name }
with
it { is_expected.to validate_presence_of(:name) }
I hope this will work.
.....
it { should validate_presence_of :name }
.....
You must delete character 'S' from 'validateS_presence_of' in all strings
Instead of it it { should validates_presence_of :name }
write it { should validate_presence_of :name }

Rails Tutorial User.save

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.

Getting an error when inserting a new record to the database

i am using Rails 3.2.3 on Ubuntu and below is my schema file and model file code:
schema.rb
ActiveRecord::Schema.define(:version => 20120528062318) do
create_table "users", :force => true do |t|
t.string "name"
t.string "email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "password_digest"
end
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
end
user.rb
class User < ActiveRecord::Base
attr_accessible :email, :name, :password, :password_confirmation
has_secure_password
before_save { |user| user.email = email.downcase }
valides :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: true
validates :password, presence: true, length: { minimum: 6 }
validates :password_confirmation, presence: true
end
i tried to enter a new record to the database from the rails console with the command: User.create(name: 'Hilal Agil', email: 'hilaal#me.com', password: 'welcome', password_confirmation: 'welcome') and i am getting the error below:
NoMethodError: undefined method `valides' for #<Class:0xaeba6cc>
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.3/lib/active_record/dynamic_matchers.rb:50:in `method_missing'
from /home/hilarl/workspace/twitster/app/models/user.rb:17:in `<class:User>'
from /home/hilarl/workspace/twitster/app/models/user.rb:12:in `<top (required)>'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `load'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:469:in `block in load_file'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:639:in `new_constants_in'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:468:in `load_file'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:353:in `require_or_load'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:502:in `load_missing_consta
nt'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in `block in const_miss
ing'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `each'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):2
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:47:in `start'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands/console.rb:8:in `start'
from /home/hilarl/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.3/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3p194 :003 >
Any idea what i am doing wrong here? thanks.
You have a typo.
This line:
valides :name, presence: true, length: { maximum: 50 }
Should say this:
validates :name, presence: true, length: { maximum: 50 }
If you look at the error message you can see that it is telling you that you are trying to call a method name valides, which does not exist.

Resources