Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

General Discussion

Luke Ward
Luke Ward
9,142 Points

Cant get validate probile_name to work.

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

I use this code but everytime i try and make a profile name no matter what I put it will still tell me it is not formated corectly.

4 Answers

Did you mean:

/a-zA-Z-+/ ?

It might be treating the - between Z and \ as a 'range'.

Try escaping the - with \ to get \- and see if that is the cause?

Robert Gray
Robert Gray
6,674 Points

I am having the same issue:

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

user.profile_name = "My Profile with Spaces"
assert user.errors[:profile_name].include?("Must be Formatted Correctly")

This assertion still fails

Luke Ward
Luke Ward
9,142 Points

I think he does try to fix it in a later video from what I can remember I have only just come back to this project so if I found out how to do it ill let you know.

Jeremy G
Jeremy G
4,601 Points

Use this code instead for the keyspace

/\A[a-zA-Z0-9_-]+\Z/