Last active
December 31, 2015 13:48
-
-
Save guinslym/7994888 to your computer and use it in GitHub Desktop.
Ruby Error with in the Regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest' #for password hasing | |
class User < ActiveRecord::Base | |
#It tells Ruby to create reader and writer methods for password. Because the password | |
#column doesn’t exist in your table anymore, a password method isn’t created automatically | |
#by Active Record. Still, you need a way to set the password before it’s encrypted, so you make | |
#your own attribute to use. This works like any model attribute, except that it isn’t persisted to | |
#the database when the model is saved. | |
attr_accessor :password | |
validates_uniqueness_of :email | |
validates_length_of :email, :within => 5..50 | |
validates :email, :uniqueness => true, | |
:length => { :within => 5..50 }, | |
:format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i } | |
validates_confirmation_of :password | |
validates_length_of :password, :within => 4..20 | |
validates_presence_of :password, :if => :password_required? | |
#And it’s | |
#required only if this is a new record (the hashed_password attribute is blank) or if the password | |
#accessor you created has been used to set a new password (password.present?) | |
has_one :profile | |
has_many :articles, -> { order('published_at DESC, title ASC') }, | |
:dependent => :nullify | |
has_many :replies, :through => :articles, :source => :comments | |
#applies to all operations that trigger a save, including create and update. | |
before_save :encrypt_new_password | |
def self.authenticate(email, password) | |
user = find_by_email(email) | |
return user if user && user.authenticated?(password) | |
end | |
#This is a simple predicate method that checks to make sure the stored | |
#hashed_password matches the given password after it has been encrypted (via encrypt). If it | |
#matches, #true is returned. | |
def authenticated?(password) | |
self.hashed_password == encrypt(password) | |
end | |
protected | |
def encrypt_new_password | |
return if password.blank? | |
self.hashed_password = encrypt(password) | |
end | |
#which returns true if a password is required or false if it’s not | |
def password_required? | |
hashed_password.blank? || password.present? | |
end | |
# for PRODUCTIONO use bcrypt gem (https://github.com/codahale/bcrypt-ruby) instead of SHA1. | |
def encrypt(string) | |
Digest::SHA1.hexdigest(string) | |
end | |
end | |
user= User.first | |
ArgumentError: The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option? | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validations/format.rb:46:in `check_options_validity' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validations/format.rb:20:in `check_validity!' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validator.rb:143:in `initialize' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validations/with.rb:87:in `new' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validations/with.rb:87:in `block in validates_with' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validations/with.rb:86:in `each' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validations/with.rb:86:in `validates_with' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validations/validates.rb:119:in `block in validates' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validations/validates.rb:109:in `each' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activemodel-4.0.1/lib/active_model/validations/validates.rb:109:in `validates' | |
from /home/guinslym/Documents/programming/rails_book/ch4/blog/app/models/user.rb:13:in `<class:User>' | |
from /home/guinslym/Documents/programming/rails_book/ch4/blog/app/models/user.rb:2:in `<top (required)>' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:424:in `load' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:424:in `block in load_file' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:616:in `new_constants_in' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:423:in `load_file' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:324:in `require_or_load' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:463:in `load_missing_constant' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.1/lib/active_support/dependencies.rb:184:in `const_missing' | |
from (irb):8 | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.1/lib/rails/commands/console.rb:90:in `start' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.1/lib/rails/commands/console.rb:9:in `start' | |
from /home/guinslym/.rvm/gems/ruby-2.0.0-p353/gems/railties-4.0.1/lib/rails/commands.rb:62:in `<top (required)>' | |
from bin/rails:4:in `require' | |
from bin/rails:4:in `<main>'2.0.0-p353 :009 > | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment