Rails 3: Error with Devise Gem + :uniqueness validaiton - ruby-on-rails-3

I'm running rails 3.2.5 with the latest Devise gem. And i have a user class that is related to an Actor class. There is a field called slug that validates fine if I use the following syntax...
validates :slug, :presence => true, :length => { :maximum => 50 }
But if I place the extra uniqueness argument
validates :slug, :presence => true, :length => { :maximum => 50 }, :uniqueness => true
it throws an error...
NoMethodError in Devise::RegistrationsController#update
undefined method `text?' for nil:NilClass
I'm not sure what is causing such.
I should also mention that in my user model class I have the following...
attr_accessible :name, :email, :password, :password_confirmation, :language, :remember_me, :profile_attributes, :admin, :slug
And that in my config file I have
config.active_record.whitelist_attributes = false #not standard nor recommended I know

I had the same problem and was able to get around it by separating out validates_uniqueness_of by itself. So for me
validates :username, :presence, uniqueness: { case_sensitive: false }
throws the same error (undefined method 'text?'). while changing to
validates_presence_of :username
validates_uniqueness_of :username, { case_sensitive: false }
works just fine.

Related

Rails nil value when saving to database

I am trying to implement a user authentication system inside rails, this is my model:
class User < ActiveRecord::Base
attr_accessible :id, :email, :name, :password, :created_at, :updated_at
has_secure_password
before_save { email.downcase! }
validates :email, presence: true, :uniqueness => { :case_senstive => false }
validates :name, presence: true
validates :password, presence: true, length: { minimum: 6 }
end
Running in the console i can read the User table successfully, then when i try to create a record:
User.new(:name => "A", :email => "a#a.a", :password => "password")
running valid on it retrurns true, but when saving the record, i get error:
users.password may not be NULL
Extracting the password out of the hash works fine.
What is the problem?
Thanks
Where are you setting :password_confirmation? The example in the docs suggests you need it.
http://api.rubyonrails.org/classes/ActiveModel/SecurePassword/ClassMethods.html
Main problem is in has_secure_password. If you are using that you should have string field :password_digest (it's where your encrypted password will be saved). And you need delete field :password and your validator of presence for that too. After add gem 'bcrypt-ruby', '~> 3.0.0' .
And now that should work
user = User.new(:name => "A", :email => "a#a.a",
:password => "password", :password_confirmation => "password")
But better use great gem https://github.com/plataformatec/devise . It has everything you need.
NEW ADDED
you should have attr_accessible for :password_confirmation too and that field should be used in your form.
more info about has_secure_password

Rails validation error email is too short, email is invalid

In attempting to seed my database I ran into a validation error on my User model's email attribute. The error:
Validation failed: Email is too short (minimum is 5 characters), Email is invalid
The thing is, my email is xxxxxxxx#gmail.com. I have five characters. Sorry for the beginner question but I don't know what is going on. I recently followed Railscasts to reset a User's password, and enable CanCan. I'm not sure if CanCan would affect anything, but prior to exploring that new functionality I've been able to fully seed my database without problems. I've pasted in some of my code below. I'm running Rails 3.0.5 and Ruby 1.9.2.
An example of how I create a User in my seed file:
me = User.create(:email => 'me#gmail.com', :password => 'test', :profile => my_profile)
User.rb model:
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :password, :password_confirmation
before_save :encrypt_new_password
before_create { generate_token(:auth_token) }
before_validation :downcase_email
has_one :profile, :dependent => :destroy
accepts_nested_attributes_for :profile
validates :email, :uniqueness => true,
:length => { :within => 5..50 },
:format => { :with => /^[^#][\w.-]+#[\w.-]+[.][a-z]{2,4}$/i }
validates :password, :confirmation => true,
:length => { :within => 4..20 },
:presence => true,
:if => :password_required?
Add :email to attr_accessible to allow mass assignment on it. Without that the email field will not even be set so validation will fail.

rails3.1.1 and sunspot: Solr Response: Not Found error

I'm trying to build an application with sunspot_solr and I keep getting this error:
rsolr (0.12.1) lib/rsolr/connection/requestable.rb:39:in `request'
rsolr (0.12.1) lib/rsolr/client.rb:34:in `request'
sunspot (1.2.1) lib/sunspot/search/abstract_search.rb:35:in `execute'
sunspot_rails (1.2.1) lib/sunspot/rails/searchable.rb:306:in `solr_execute_search'
sunspot_rails (1.2.1) lib/sunspot/rails/searchable.rb:139:in `solr_search'
app/controllers/search_controller.rb:7:in `results'
Here is my sunspot.yml:
solr:
hostname: localhost
port: 8980
log_level: INFO
path: /solr
pid_dir: /solr/pids/development
auto_commit_after_delete_request: true
And the solr instance at http://localhost:8980/solr/admin/ does work, I get all the desired results when I run the queries there. But when I run through my application I get the above error.
My configuration:
Rails 3.1.1
Sunspot: 1.2.1
Please let me know what other information you need from me and thanks in advance for the answers.
I went through the logs files and found out this error:
DEPRECATION WARNING: class_inheritable_attribute is deprecated, please use class_attribute method instead. Notice their behavior are slightly different, so refer to class_attribute documentation first. (called from <class:User> at /home/romio/hummingtown_v1/app/models/user.rb:17)
Here is my user.rb:
require 'digest/sha2'
class User < ActiveRecord::Base
has_many :userauths, :dependent => :destroy
attr_accessor :password
attr_accessible :first_name, :last_name, :email, :password, :salt
email_regex = /\A[\w+\-.]+#[a-z\d\-.]+\.[a-z]+\z/i
validates :first_name, :presence => true, :length => { :maximum => 50 }
validates :last_name, :presence => true, :length => { :maximum => 50 }
validates :email, :presence => true, :format => { :with => email_regex },
:uniqueness => { :case_sensitive => false }
searchable :auto_index => true, :auto_remove => true do
text :first_name, :default_boost => 2
text :last_name
text :email
end

Model validation

I got the following example from http://guides.rubyonrails.org/getting_started.html
models/post.rb
class Post < ActiveRecord::Base
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
end
but it doesn't work
NoMethodError in PostsController#index
undefined method `validates' for #<Class:0x7f1fd7b6d750>
It was a version issue. Installing Rails 3 solved the problem.

Rails 3: validates :presence => true vs validates_presence_of

What is the difference between validates :presence and validates_presence_of? Looking through ActiveModel it looks like they setup the validation the same way. However, given the following model definition:
class Account < ActiveRecord::Base
has_one :owner_permission, :class_name => 'AccountPermission', :conditions => { :owner => true, :admin => true }
has_one :owner, :class_name => 'User', :through => :owner_permission, :source => :user
validate :owner, :presence => true
validates_associated :owner
end
Calling save on an instance of Account does not validate the presence of owner. Though, if I use validates_presence_of it will.
All those validates_whatever_of :attr macros do is call validates :attr, :whatever => true.
The problem is you are using validate and not validates.
In Rails 3.x and 4.x - it is now encouraged to use the following syntax:
validates :email, presence: true
validates :password, presence: true
Instead of the 2.x way:
validates_presence_of :email
validates_presence_of :password
In fact validates and validates_presence_of is not entirely equal !
validates_presence_of is allowing you to also lazily check by example of the value in the field is included in another table.
Like that:
validates_presence_of :pay_type, :inclusion => PaymentType.names
Which is something you can't do as easily with something like that
validates :pay_type, presence, :inclusion => PaymentType.names
Cause the inclusion is only evaluated the first time (not in a lazy way)
I would have thought that it is appropriate to use validates :foo presence: true when you want to include other validations of :foo such as length or uniqueness. But if you know the only validation you'll need for an attribute is presence, then validates_presence_of appears to be more efficient.
So:
validates :foo, length: {maximum: 50}, uniqueness: true,
format: {with: /bar/},
presence: true # lots of validations needed
But:
validates_presence_of :foo # only presence validation needed