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 trialKevin Kenger
32,834 PointsA 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
42,016 Pointsit 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."
}
Stone Preston
42,016 Pointswhat error are you getting?
Kevin Kenger
32,834 Points1) 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
42,016 Pointswhat assertion do you have at line 38 of your user_test
Kevin Kenger
32,834 Pointsassert !user.errors[:profile_name].empty?
Kevin Kenger
32,834 PointsWell 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
42,016 Pointsyeah unfortunately the rails error messages that get spit out usually arent very easy to read
Kevin Kenger
32,834 PointsKevin Kenger
32,834 PointsI initially did that (and just retried it) and it gave me all of this:
Stone Preston
42,016 PointsStone Preston
42,016 Pointshmm maybe try this then:
Kevin Kenger
32,834 PointsKevin Kenger
32,834 PointsI 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
42,016 PointsStone Preston
42,016 Pointsyou 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.