Skip to content

Instantly share code, notes, and snippets.

@cantlin
Created March 24, 2011 21:21
Show Gist options
  • Save cantlin/885925 to your computer and use it in GitHub Desktop.
Save cantlin/885925 to your computer and use it in GitHub Desktop.
# home.erb.html
<% @current_user.following.each do |user| %>
<%= user['status'] %><br /><br />
<% end %>
# @current_user.following is a hash from JSON.parse(twitter_response)
# the above returns something like the following once per user
{"in_reply_to_status_id_str"=>nil, "text"=>"This Week In Dylan Lewis John Martin, a must-listen for anyone who follows @dylanljmartin http://thisweekindljm.podomatic.com", "contributors"=>nil, "retweeted"=>false, "in_reply_to_user_id_str"=>nil, "retweet_count"=>0, "id_str"=>"47517965167505408", "source"=>"web", "geo"=>nil, "truncated"=>false, "created_at"=>"Tue Mar 15 04:42:20 +0000 2011", "place"=>nil, "in_reply_to_status_id"=>nil, "favorited"=>false, "coordinates"=>nil, "id"=>47517965167505408, "in_reply_to_screen_name"=>nil, "in_reply_to_user_id"=>nil}
# trying to refer to the nested value of 'text'
<% @current_user.following.each do |user| %>
<%= user['status']['text'] %><br /><br />
<% end %>
# returns
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
# the following works
<%= @current_user.following[0]['status']['text'] %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment