wz4mfa
Last Updated: February 25, 2016
·
1.662K
· rebyn
3

Avoid nil:NilClass error message

Sometimes your Rails app may throw this error:

NoMethodError in Articles#show
undefined method `comment' for nil:NilClass

The cleanest workaround is to add try(:field) </code> to your code, like:

<%= @article.try(:comment) %>

In some cases whereas information are such crucial and must-display like Name</code> or Profile picture</code> it's crucial you put some validations on the model itself.

  • T.
Say Thanks
Respond