Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Issues with regular expression

Here is the test we ran:

test "a user can have a correctly formatted profile name" do 
  user = User.new(first_name: 'Joel', last_name: 'Crouse', email:   'crousejoel@gmail.com')
  user.password = user.password_confirmation = '84948494'
  user.profile_name = 'Joey'
  assert user.valid?
end

Here is my user.rb file that validates the profile_name:


validates :profile_name,
                presence: true,
                uniqueness: true,
                format: {
                  with: /^[a-ZA-Z0-9_-]+$/,
                }

and here is the error i get in my terminal:


Rack::File headers parameter replaces cache_control after Rack 1.5. /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:in require': /Users/joelcrouse/upNetwork/app/models/user.rb:18: empty range in char class: /^[a-ZA-Z0-9_-]+$/ (SyntaxError) from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:inblock in require' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in load_dependency' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:251:inrequire' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:359:in require_or_load' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:502:inload_missing_constant' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:192:in block in const_missing' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:190:ineach' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:190:in const_missing' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/inflector/methods.rb:230:inblock in constantize' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/inflector/methods.rb:229:in each' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/inflector/methods.rb:229:inconstantize' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:554:in get' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:588:inconstantize' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise.rb:274:in get' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise/mapping.rb:77:into' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise/mapping.rb:72:in modules' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise/mapping.rb:89:inroutes' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise/mapping.rb:156:in default_used_route' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise/mapping.rb:66:ininitialize' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise.rb:308:in new' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise.rb:308:inadd_mapping' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise/rails/routes.rb:208:in block in devise_for' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise/rails/routes.rb:207:ineach' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise/rails/routes.rb:207:in devise_for' from /Users/joelcrouse/upNetwork/config/routes.rb:2:inblock in <top (required)>' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:282:in instance_exec' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:282:ineval_block' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:260:in draw' from /Users/joelcrouse/upNetwork/config/routes.rb:1:in<top (required)>' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:in load' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:inblock in load' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:236:in load_dependency' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/dependencies.rb:245:inload' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/application/routes_reloader.rb:40:in block in load_paths' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/application/routes_reloader.rb:40:ineach' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/application/routes_reloader.rb:40:in load_paths' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/application/routes_reloader.rb:16:inreload!' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/application.rb:108:in reload_routes!' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/devise-3.0.0/lib/devise/rails.rb:14:inblock in <class:Engine>' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:34:in call' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:34:inexecute_hook' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:43:in block in run_load_hooks' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:42:ineach' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.12/lib/active_support/lazy_load_hooks.rb:42:in run_load_hooks' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/application/finisher.rb:52:inblock in <module:Finisher>' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/initializable.rb:30:in instance_exec' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/initializable.rb:30:inrun' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/initializable.rb:55:in block in run_initializers' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/initializable.rb:54:ineach' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/initializable.rb:54:in run_initializers' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/application.rb:136:ininitialize!' from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.12/lib/rails/railtie/configurable.rb:30:in method_missing' from /Users/joelcrouse/upNetwork/config/environment.rb:5:in<top (required)>' from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in require' from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:inrequire' from /Users/joelcrouse/upNetwork/test/test_helper.rb:2:in <top (required)>' from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:inrequire' from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in require' from test/unit/user_test.rb:1:in<main>'


Any Ideas?

if this is the reg expression from the video its wrong. Under the video he posted a reg slightly different reg expression that might solve your problem

5 Answers

Nick's answer is incomplete, the regex should have a 0-9 in it like so:

validates :profile_name, format: {
  with: /\A[a-zA-Z0-9\-\_]+\Z/,
  message: "must be formatted correctly."
}
Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

Hi Prosper Hughie,

What Andre' said is correct, Jason provided a slightly different code example with the video.

validates :profile_name, format: {
  with: /\A[a-zA-Z\-\_]+\Z/,
  message: "must be formatted correctly."
}

I'm not sure if this will fix your issue, but Jason Seifer might be able to give you a better answer. :)

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Try making the first "Z" lower case in the regular expression. That seems to be the issue here. Also, we fix it a little later in the videos :)

Is there a video that explains regex Jason Seifer ?

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hashim Warren we don't have any regex only videos. Great idea, though!