Code Monkey home page Code Monkey logo

Comments (7)

cjheath avatar cjheath commented on September 5, 2024

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.

anonrig avatar anonrig commented on September 5, 2024

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.

cjheath avatar cjheath commented on September 5, 2024

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.

anonrig avatar anonrig commented on September 5, 2024

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.

cjheath avatar cjheath commented on September 5, 2024

Ok, that's new evidence - you hadn't established that before. Wierd... I'll investigate.

from geoip.

anonrig avatar anonrig commented on September 5, 2024

What can I do to help?

from geoip.

cjheath avatar cjheath commented on September 5, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.