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.
Written by Tu Hoang
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#