get city, country and ip from a visitor on production mode with rails 3.2 #239

Closed
hyperrjas opened this Issue May 21, 2012 · 12 comments

Comments

Projects
None yet
2 participants

I have installed http://www.rubygeocoder.com/ in a project rails in a production server but this gem in my production server not works fine for me...

I write these methods in views in my development environment and works fine:

<%= request.ip %>   
<%= request.location.city %>    
<%= request.location.country %>

and I get: 127.0.0.1 Reserved

this result its usually because I am working in localhost.

However after make a deploy with capistrano in the production server in linode I get error with these methods, I can see in my production log the next error:

Completed 500 Internal Server Error in 909ms

ActionView::Template::Error (undefined method `location' for #<ActionDispatch::Request:0x00000005aa8570>):

Why I get this error in my production server for these methods?

Thank you!

Owner

alexreisner commented May 21, 2012

Are you sure the gem is installed on your production server? Can you run Geocoder.search(...) in a console on the server?

Yeahh I run this in my linode server:

hyperrjas@domain:~/apps/app_name/current$ rails c production
Loading production environment (Rails 3.2.3)
irb(main):001:0> results = Geocoder.search("McCarren Park, Brooklyn, NY")

and I get:

=> [#<Geocoder::Result::Google:0x00000005982998 @data={"address_components"=>[{"long_name"=>"McCarren Park", "short_name"=>"McCarren Park", "types"=>["establishment"]}, {"long_name"=>"780", "short_name"=>"780", "types"=>["street_number"]}, {"long_name"=>"Lorimer St", "short_name"=>"Lorimer St", "types"=>["route"]}, {"long_name"=>"Greenpoint", "short_name"=>"Greenpoint", "types"=>["neighborhood", "political"]}, {"long_name"=>"Brooklyn", "short_name"=>"Brooklyn", "types"=>["sublocality", "political"]}, {"long_name"=>"New York", "short_name"=>"New York", "types"=>["locality", "political"]}, {"long_name"=>"Kings", "short_name"=>"Kings", "types"=>["administrative_area_level_2", "political"]}, {"long_name"=>"New York", "short_name"=>"NY", "types"=>["administrative_area_level_1", "political"]}, {"long_name"=>"United States", "short_name"=>"US", "types"=>["country", "political"]}, {"long_name"=>"11222", "short_name"=>"11222", "types"=>["postal_code"]}], "formatted_address"=>"McCarren Park, 780 Lorimer St, Brooklyn, NY 11222, USA", "geometry"=>{"bounds"=>{"northeast"=>{"lat"=>40.7234513, "lng"=>-73.9485505}, "southwest"=>{"lat"=>40.7188105, "lng"=>-73.9560237}}, "location"=>{"lat"=>40.7220387, "lng"=>-73.9514422}, "location_type"=>"APPROXIMATE", "viewport"=>{"northeast"=>{"lat"=>40.7304946, "lng"=>-73.9354348}, "southwest"=>{"lat"=>40.7135817, "lng"=>-73.9674496}}}, "types"=>["park", "establishment"]}>]

I am as database mongodb and as odm mongoid :)

one different with linode server is that linode server is ubuntu 64 bits and my local ubuntu is 32 bits... Can be this the problem?

Thank you very much!

Owner

alexreisner commented May 21, 2012

I don't think it's a 32-vs-64-bit issue. The problem is that the location method is not being added to Rack::Request. Can you check whether some things are defined in your production console?:

defined?(Rack::Request)
defined?(Geocoder::Request)

and also post the result of the following:

Rack::Request.included_modules

Hi, Thank you for your response. I paste the results:

irb(main):001:0> defined?(Rack::Request)
=> "constant"
irb(main):002:0> defined?(Geocoder::Request)
=> "constant"
irb(main):003:0> Rack::Request.included_modules
=> [Geocoder::Request, PP::ObjectMixin, ActiveSupport::Dependencies::Loadable, Mongoid::Extensions::Object::Yoda, Mongoid::Extensions::Object::Substitutable, Mongoid::Extensions::Object::Reflections, Mongoid::Extensions::Object::DeepCopy, Mongoid::Extensions::Object::Checks, JSON::Ext::Generator::GeneratorMethods::Object, Kernel]
irb(main):004:0> 

Thank you very much again @alexreisner :)

Owner

alexreisner commented May 21, 2012

Everything looks right. I have to ask a dumb question: are you sure the app server was restarted since you added Geocoder?

you say restart my machine ubuntu so in linode? or restart my nginx + unicorn combo server?

Thank you!

Owner

alexreisner commented May 21, 2012

Restart the production app server--Unicorn. I assumed Capistrano would have done that...

yes the server is restarted each time I make a new deploy :D.

This is my task for restart:

namespace :deploy do
  %w[start stop restart].each do |command|
    desc "#{command} unicorn server"
    task command, roles: :app, except: {no_release: true} do
      run "/etc/init.d/unicorn_#{application} #{command}"
    end
  end

In my caspitrano log you can see:

transaction: commit
  * executing `deploy:restart'
  * executing "/etc/init.d/unicorn_app restart"

Thank you again for your help @alexreisner :)

Owner

alexreisner commented May 21, 2012

Hmmm...this is hard to diagnose through messages. My advice is to think about all the differences between your development and production environments and whether each difference could be causing the problem. You might also want to print the output of Rack::Request.included_modules in your template and make sure it matches what you got in your console.

This is my output in my template in development environment for:

[Geocoder::Request, PP::ObjectMixin, ActiveSupport::Dependencies::Loadable, Mongoid::Extensions::Object::Yoda, Mongoid::Extensions::Object::Substitutable, Mongoid::Extensions::Object::Reflections, Mongoid::Extensions::Object::DeepCopy, Mongoid::Extensions::Object::Checks, JSON::Ext::Generator::GeneratorMethods::Object, FactoryGirl::Syntax::Vintage, Kernel]  

and this is my output in production environment in my linode server:

  [PP::ObjectMixin, ActiveSupport::Dependencies::Loadable, Mongoid::Extensions::Object::Yoda, Mongoid::Extensions::Object::Substitutable, Mongoid::Extensions::Object::Reflections, Mongoid::Extensions::Object::DeepCopy, Mongoid::Extensions::Object::Checks, JSON::Ext::Generator::GeneratorMethods::Object, Kernel]      

I can see some things are differents...

I can not see in production:

 Geocoder::Request
Owner

alexreisner commented May 21, 2012

Right, so, Geocoder::Request is not included in Rack::Request for the server, even though it is included when you run the console. What if you reboot the whole server? If that doesn't work, maybe put this in an initializer:

Rack::Request.send :include, Geocoder::Request

Woow the problem was fixed :D. I had restart my linode server and now does work fine :D.

The problem was restart the server :D.

I think that if is possible add a note on instruction for production server, for that the people restart the server after install geocoder gem :D.

Thank you very much again @alexreisner for your gem and for your help.

Thank you very much :D.

Regards!

hyperrjas closed this May 21, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment