Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby on Rails full_messages for specific attribute

In Rails I can do this to get the full error message (includes the name of the attribute)

book.errors.full_messages.each do |err|
  puts err
end

My question is, how can I do the same thing but for a specific attribute?

I can't do this:

book.errors[:title].full_messages

As for now this is what I'm doing

book.errors[:title].each do |err|
  puts " Title #{err}"
end

I'm looking forward if there's a better way

like image 750
Agung Setiawan Avatar asked Nov 02 '25 22:11

Agung Setiawan


1 Answers

To get the full error message for a specific attribute use full_messages_for:

book.errors.full_messages_for(:title)
like image 155
infused Avatar answered Nov 05 '25 14:11

infused