A beginner with no programming experience, I finished Progate’s Ruby on Rails course a while ago.
When I reviewed it while doing the Rails tutorial, when I was doing Progate, it didn’t come to my attention very much, but now I can understand a little more, so I would like to summarize the points I noticed. This article is part 2. Click here for Part 1 https://qiita.com/HiMinmeg/items/1e29bf9252096d463f6b
I hope this is useful for those who are doing Progate but don’t know what to expect.
error message each sentence [Course V]
After validation, when an error occurs in the post (such as entering more than the limited number of characters…) When you post, an error message will be displayed.
At this time, I didn’t understand the reason for using each statement. It was obvious when I thought about it, but I was happy when I understood it, so I would like to record it.
The error message each statement is used as an example.
<% @post.errors.full_messages.each do |message| %> <%= message %> <% end %>
errors.full_messages gets the error contents as an array.
Reference article https://qiita.com/ryuuuuuuuuuu/items/1a1e53d062bff774d88a [Rails Guide 7. Responding to validation errors](https://railsguides.jp/active_record_validations.html#%E3%83%90%E3%83%AA%E3%83%87%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%A8%E3%83%A9%E3%83%BC%E3%81%AB%E5%AF%BE%E5%BF(%9C%E3%81%99%E3%82%8B) [Rails Guide 8. Display validation error in view](https://railsguides.jp/active_record_validations.html#%E3%83%90%E3%83%AA%E3%83%87%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3%E3%82%A8%E3%83%A9%E3%83%BC%E3%82%92%E3%83%93%E3(%83%A5%E3%83%BC%E3%81%A7%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B)
The point here is that if multiple validations are applied, there are also multiple arrays to be retrieved with the error content. Therefore, it is necessary to retrieve the contents of the array one by one with each statement. There was a possibility that multiple errors could occur at the same time, so I was wondering why it was each statement because I didn’t think about it at all.
errors.full_messagesgets the error contents as an array.
- If multiple validations are applied, there are multiple arrays that can be obtained as error contents, so it is necessary to fetch the array contents one by one with each statement.
Thank you for reading this far. If you’re wrong, please let us know in the comments. I hope it will be helpful.