2

Without getting into all the javascript for a textfield, I'd like the following validation:

validates_length_of :brief_description,
  :maximum=>250, 
  :message => "Brief is #{self.brief_description.length} long (max is 250)"

But the self.brief_description reference in the message doesn't work. What's the best way to do this?

I tried #{params[:brief_description].length} as well, but the model doesn't know anything about params...

1 Answer 1

1

I could be wrong but I think you can try enclosing the string in single-quotes, and it might defer evaluating the string until runtime.

Short of that, you could write a custom validation (that doesn't use the validates_length_of macro) and do something like:

def validate_brief_description
  errors.add :brief_description, "Brief is #{brief_description.length} long (max is 250)" if brief_description.length > 250
end
Sign up to request clarification or add additional context in comments.

1 Comment

The single quotes prevented the #{} from being evaluated, but the custom validation did the trick. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.