Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3: how to generate custom error message from failed validation

I'm using

validates :feed_id, presence: true, uniqueness: true

How should I be generating a custom error message to specify that the user has already subscribed to this feed (the feed_id) field is a duplicate

I know I can just do validate_uniqueness_of but it would clutter up the code unnecessarily. How do I pass a specific error message if uniqueness validation fails??

like image 986
Ken W Avatar asked May 07 '12 19:05

Ken W


1 Answers

Put a hash with the key message and desired message as the value instead of true:

validates :feed_id, presence: true, uniqueness: {message: "already subscribed"}
like image 183
jdoe Avatar answered Sep 28 '22 06:09

jdoe