dcalixto
dcalixto

Reputation: 411

undefined method `keys' for nil:NilClass

Why when you iterate through a serialized JSON hash but spits out that the class is nil?

View

<% @test.yo.keys.each do |key| %>
  <%= key %>
<% end %>

Model

class Test < ActiveRecord::Base
  belongs_to :city
  serialize :yo, JSON
end

Upvotes: 0

Views: 5343

Answers (1)

Chris Drappier
Chris Drappier

Reputation: 5370

it's saying that yo is nil for @test. you need to check yo for nil before you call a method against it :

<% if [email protected]? %>
  <% @test.yo.keys.each do |key| %>
    <%= key %>
  <% end %>
<% end %>

you can probably find a better place to put your check, but this gives you an idea of how to fix it

Upvotes: 1

Related Questions