Comments (7)
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 [email protected] 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.
from geoip.
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 <[email protected] (mailto:[email protected])> 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)).
from geoip.
On 11/07/2013, at 8:46 AM, ynizipli [email protected] 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.
from geoip.
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.
from geoip.
Ok, that's new evidence - you hadn't established that before. Wierd... I'll investigate.
from geoip.
What can I do to help?
from geoip.
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.
from geoip.
Related Issues (20)
- GeoIP.asn incorrect parsing of some records HOT 4
- Doesn't work with recent version of db HOT 2
- IP2Location LITE Support HOT 2
- Not supported with Maxmind GeoIP2 Country HOT 4
- Include CHANGELOG file to this gem HOT 2
- MMDB format will be supported in furture? HOT 1
- Errno::ENOENT: No such file or directory @ rb_sysopen - GeoIP.dat HOT 2
- Release 1.6 HOT 6
- utf-8 read incorrect HOT 1
- dnsmasq HOT 1
- Very weird bug. Sometime GEO_IP_CITY.city returns incorrect info HOT 1
- .each method always returns empty Enumerable for GeoIPCity.dat database HOT 10
- Missing Hash Outputs for different lookups
- missing 1.6.3 version tag in GitHub HOT 4
- Why is the latitude/longitude an empty string? HOT 1
- DATA_DIR incompatible with kali-rolling HOT 1
- Version unclear HOT 1
- GeoLite Legacy databases are now discontinued HOT 13
- test/csvORG2dat.py does not support python3 HOT 1
- Reporting Abuse on GeoIP HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from geoip.