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 Build a Simple Ruby on Rails Application Building the Profile Page Testing the Profile Name

Kevin Kenger
Kevin Kenger
32,834 Points

A user should have a profile name without spaces

For some reason, I'm getting this error after I change the format from <code>/a-zA-Z0-9_-/</code> to /[a-zA-Z0-9_-]+/ in the user.rb file.

Any help is greatly appreciated!

user.rb

  validates :profile_name, presence: true,
                           uniqueness: true,
                           format: {
                            with: /[a-zA-Z0-9_-]+/,
                            message: "Must be formatted correctly."
                           }

user_test.rb

  test "a user should have a profile name without spaces" do
    user = User.new
    user.profile_name = "My Profile With Spaces"

    assert !user.save
    assert !user.errors[:profile_name].empty?
    assert user.errors[:profile_name].include?("Must be formatted correctly.")
  end

  test "a user can have a correctly formatted profile name" do
    user = User.new(first_name: "Kevin", last_name: "Kenger", email: "kengers@icloud.com")
    user.password = user.password_confirmation = "asdfghjkl"

    user.profile_name = "kevinkenger"
    assert user.valid?
  end

4 Answers

Stone Preston
Stone Preston
42,016 Points

it looks like the code for the regex in the teachers notes isnt matching up with the code used in the video. in the teachers notes he has what you have: /[a-zA-Z0-9_-]+/. However, in the video he uses /^[a-zA-Z0-9_-]+$/

try making that change so your regex matches whats in the video in the user file:

validates :profile_name, presence: true,
                           uniqueness: true,
                           format: {
                            with: /^[a-zA-Z0-9_-]+$/,
                            message: "Must be formatted correctly."
                           }
Kevin Kenger
Kevin Kenger
32,834 Points

I initially did that (and just retried it) and it gave me all of this:

/Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validations/format.rb:39:in `check_options_validity': 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? (ArgumentError)
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validations/format.rb:20:in `check_validity!'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validator.rb:157:in `initialize'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validations/with.rb:89:in `new'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validations/with.rb:89:in `block in validates_with'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validations/with.rb:88:in `each'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validations/with.rb:88:in `validates_with'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validations/validates.rb:121:in `block in validates'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validations/validates.rb:111:in `each'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activemodel-4.1.0/lib/active_model/validations/validates.rb:111:in `validates'
    from /Users/kengers/kconnect/app/models/user.rb:14:in `<class:User>'
    from /Users/kengers/kconnect/app/models/user.rb:1:in `<top (required)>'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `block in require'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:247:in `require'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:348:in `require_or_load'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:480:in `load_missing_constant'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:180:in `const_missing'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/inflector/methods.rb:238:in `const_get'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/inflector/methods.rb:238:in `block in constantize'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/inflector/methods.rb:236:in `each'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/inflector/methods.rb:236:in `inject'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/inflector/methods.rb:236:in `constantize'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:552:in `get'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:583:in `constantize'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise.rb:297:in `get'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise/mapping.rb:77:in `to'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise/mapping.rb:72:in `modules'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise/mapping.rb:89:in `routes'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise/mapping.rb:156:in `default_used_route'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise/mapping.rb:66:in `initialize'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise.rb:331:in `new'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise.rb:331:in `add_mapping'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise/rails/routes.rb:221:in `block in devise_for'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise/rails/routes.rb:220:in `each'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/devise-3.2.4/lib/devise/rails/routes.rb:220:in `devise_for'
    from /Users/kengers/kconnect/config/routes.rb:2:in `block in <top (required)>'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/routing/route_set.rb:335:in `instance_exec'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/routing/route_set.rb:335:in `eval_block'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/actionpack-4.1.0/lib/action_dispatch/routing/route_set.rb:313:in `draw'
    from /Users/kengers/kconnect/config/routes.rb:1:in `<top (required)>'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:40:in `each'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:40:in `load_paths'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:16:in `reload!'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:26:in `block in updater'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/file_update_checker.rb:75:in `call'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/activesupport-4.1.0/lib/active_support/file_update_checker.rb:75:in `execute'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:27:in `updater'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/application/finisher.rb:71:in `block in <module:Finisher>'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/initializable.rb:30:in `instance_exec'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/initializable.rb:30:in `run'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/initializable.rb:55:in `block in run_initializers'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/tsort.rb:345:in `each'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/tsort.rb:345:in `call'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/initializable.rb:54:in `run_initializers'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0/gems/railties-4.1.0/lib/rails/application.rb:288:in `initialize!'
    from /Users/kengers/kconnect/config/environment.rb:5:in `<top (required)>'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/kengers/kconnect/test/test_helper.rb:2:in `<top (required)>'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/kengers/.rbenv/versions/2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from test/models/user_test.rb:1:in `<main>'
Stone Preston
Stone Preston
42,016 Points

hmm maybe try this then:

validates :profile_name, presence: true,
                           uniqueness: true,
                           format: {
                            with: /^[a-zA-Z0-9_-]+$/,
                            message: "Must be formatted correctly."
                            multiline: true
                           }
Kevin Kenger
Kevin Kenger
32,834 Points

I think that worked! You're awesome, thank you so much.

Would it be too much to ask why that works? I'm still really new to Ruby and I'd like to know in case I run into another opportunity to use this! But if it's too complicated to explain I understand.

And thank you again, that was starting to drive me crazy!

PS. Is there a way for me to mark your comment as best answer? (This is my first time using the forum.)

Stone Preston
Stone Preston
42,016 Points

you cant mark comments as best answers, you can just mark the answer that all these comments are on as best if you want to. As far as to why it worked, im not sure why. The compiler said to add that line ( 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? (ArgumentError) ) , so you did, and it works now. It had something to do with the regex though. and ive only every dealt with regexes a few times in my life so I cant really tell you whats going on with it and why it didnt like it.

Stone Preston
Stone Preston
42,016 Points

what error are you getting?

Kevin Kenger
Kevin Kenger
32,834 Points

1) Failure: UserTest#test_a_user_should_have_a_profile_name_without_spaces [test/models/user_test.rb:38]: Failed assertion, no message given.

Stone Preston
Stone Preston
42,016 Points

what assertion do you have at line 38 of your user_test

Kevin Kenger
Kevin Kenger
32,834 Points

assert !user.errors[:profile_name].empty?

Kevin Kenger
Kevin Kenger
32,834 Points

Well thank you for solving my problem!

I guess next time I should actually read and try and understand what it's saying. I just got freaked out because of all that other "from Users/etc" below it!

Stone Preston
Stone Preston
42,016 Points

yeah unfortunately the rails error messages that get spit out usually arent very easy to read