Rails can't access struct variables
anonrig opened this issue · comments
Hello,
I am using Rails 4.0 and Ruby 2.0, in terminal I can easily access a IP's information, but on Rails this code gives an error to the structs variable.
<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat.country_code2 %>
In this code, "country_code2" gives an "undefined method `country_code2' for nil:NilClass" error, but when I do
<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat %>
It prints the struct properly. Is this a gem issue or a Rails misconfiguration?
I haven't tested GeoIP with Rails 4 yet, but the message
"undefined method `country_code2' for nil:NilClass"
means that your variable "lat" is nil, which will be because
the call to city() didn't find the "user.ip" that you passed.
So I suspect it's just a user error.
On 11/07/2013, at 12:32 AM, ynizipli notifications@github.com wrote:
Hello,
I am using Rails 4.0 and Ruby 2.0, in terminal I can easily access a IP's information, but on Rails this code gives an error to the structs variable.
<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat.country_code2 %>In this code, "country_code2" gives an "undefined method `country_code2' for nil:NilClass" error, but when I do
<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat %>It prints the struct properly. Is this a gem issue or a Rails misconfiguration?
—
Reply to this email directly or view it on GitHub.
Hello,
When I print the variable @lat, it prints correctly. But separating the struct and accessing its sub-variables is the part which creates an error.
Yagiz Nizipli
Sabanci University
On Thursday, July 11, 2013 at 12:40 AM, Clifford Heath wrote:
I haven't tested GeoIP with Rails 4 yet, but the message
"undefined method `country_code2' for nil:NilClass"
means that your variable "lat" is nil, which will be because
the call to city() didn't find the "user.ip" that you passed.
So I suspect it's just a user error.On 11/07/2013, at 12:32 AM, ynizipli <notifications@github.com (mailto:notifications@github.com)> wrote:
Hello,
I am using Rails 4.0 and Ruby 2.0, in terminal I can easily access a IP's information, but on Rails this code gives an error to the structs variable.
<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat.country_code2 %>In this code, "country_code2" gives an "undefined method `country_code2' for nil:NilClass" error, but when I do
<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat %>It prints the struct properly. Is this a gem issue or a Rails misconfiguration?
—
Reply to this email directly or view it on GitHub.—
Reply to this email directly or view it on GitHub (#30 (comment)).
On 11/07/2013, at 8:46 AM, ynizipli notifications@github.com wrote:
When I print the variable @lat, it prints correctly. But separating the struct and accessing its sub-variables is the part which creates an error.
Sorry, but I don't believe you. Read your original code and error message:
<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat.country_code2 %>gives an "undefined method `country_code2' for nil:NilClass"
Ruby is telling you that "lat" is nil, and nil doesn't have a country_code2 message.
Try including the debugger gem, put a "debugger" call in between those two lines,
and look at the value of lat.
You should not assume that calling "city" will always return an object that responds
to country_code2. Sometimes it returns nil, as in this case, so your code should check
for that.
Result of:
<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat %>
[ { #<struct GeoIP::City request="85.162.251.16", ip="85.162.251.16", country_code2="SK", country_code3="SVK", country_name="Slovakia", continent_code="EU", region_name="", city_name="", postal_code="", latitu......
Result of:
<% lat = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat").city("#{user.ip}") %>
<%= lat.country_code2 %>
undefined method `country_code2' for nil:NilClass
<%= lat.country_code2 %> is highlighted.
Ok, that's new evidence - you hadn't established that before. Wierd... I'll investigate.
What can I do to help?
I've updated my Ruby2.0.0 to p247, and can't reproduce your problem (but I'm not in Rails). However, the result of printing "lat" above starts with the characters [{#struct... whereas lat.inspect just says #<struct...
I honestly don't think that this is a problem with GeoIP. Something is going on within your ERB templates however, since the error message definitely indicates that "lat" is Nil in the case where "country_code2" is undefined. Perhaps put both lines inside one <%= ... %> pair?
I'l re-open this is you can show that it's really a problem with GeoIP. At the moment I have no evidence that it is.