user3277633
user3277633

Reputation: 1923

How do I validate the length of a title in a rails app?

I'm learning Ruby on Rails right now and one of the exercises that I am trying to figure out is how to validate the title such that it has more than 10 characters. The hint says to use the :length method(?) in ruby.

So far I've tried:

validates :title.length, numericality: {greater_than_or_equal_to: 10}

and

validates :title, length: {greater_than_or_equal_to: 10}

both of which has given me errors.

What should I do here?

Another quick question, what is the difference when the colon(:) is on the left and right? for the length, it's on the left (:length), but for numericality, it's on the right (numericality:) I'm thinking if it's on the left it's a variable, and if it's on the right it's a method. Not sure if that's a good way to think of it.

Upvotes: 1

Views: 283

Answers (2)

tadman
tadman

Reputation: 211580

I'm not sure where you're being told to do it that way. The documentation is pretty specific:

validates :title, length: { minimum: 10 }

Upvotes: 1

BroiSatse
BroiSatse

Reputation: 44685

Try:

validates :title, length: {minimum: 10}

To your second question:

key: value

is a hash syntax, which means the same as

:key => value

Upvotes: 3

Related Questions