Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with Rails 5 beta - Geocoder::Request can't be initialized #966

Closed
simzen85 opened this issue Jan 9, 2016 · 14 comments
Closed

Issue with Rails 5 beta - Geocoder::Request can't be initialized #966

simzen85 opened this issue Jan 9, 2016 · 14 comments

Comments

@simzen85
Copy link

simzen85 commented Jan 9, 2016

I just upgraded my app to Rails 5 beta and it seems that Geocoder::Request can't be initialized, hence request.location (using in my View) returned this

"undefined method `location' for #ActionDispatch::Request:0x007f91127f3280"

Can someone help me with this ? Tks

@TrangPham
Copy link
Contributor

Yes, could you give us more information on the bug you are seeing? Specifically:

  1. What is your rails, ruby and geocoder versions?
  2. Could you post the whole console log for your error?

Thanks

@simzen85
Copy link
Author

@TrangPham
Rails : 5.0.0.beta1
Ruby : 2.3.0
Geocoder : 1.2.14

log

App 4253 stdout: NoMethodError - undefined method location' for #<ActionDispatch::Request:0x007fcc8372b530>: App 4253 stdout: app/views/home/index.html.slim:180:inblock in _app_views_home_index_html_slim___3830065459718013949_70258207558240'
App 4253 stdout: actionview (5.0.0.beta1) lib/action_view/helpers/capture_helper.rb:39:in block in capture' App 4253 stdout: actionview (5.0.0.beta1) lib/action_view/helpers/capture_helper.rb:203:inwith_output_buffer'
App 4253 stdout: actionview (5.0.0.beta1) lib/action_view/helpers/capture_helper.rb:39:in capture' App 4253 stdout: actionview (5.0.0.beta1) lib/action_view/helpers/capture_helper.rb:153:incontent_for'

@simzen85
Copy link
Author

@TrangPham were you able to reproduce this or do you need any other info from me ? Tks

@TrangPham
Copy link
Contributor

@simzen85 I cannot reproduce the issue. I believe there is something wrong specifically your configuration and/or environment.

Steps to try to reproduce:
1 - Use ruby 2.3.0 and rails 5.0.0.beta1
2 - rails new test_project
3 - added gems to gemfile: sqlite3, redis, geocoder
4 - added geocoder config

Geocoder::Configuration.timeout = 4
Geocoder::Configuration.lookup = :google
Geocoder::Configuration.cache = Redis.new
Geocoder::Configuration.cache_prefix = "geocoder-test:"

5 - added place model

class Place < ApplicationRecord
    geocoded_by :address   # can also be an IP address
    after_validation :geocode          # auto-fetch coordinates
end
class CreatePlaces < ActiveRecord::Migration[5.0]
  def change
    create_table :places do |t|

      t.string :address
      t.float :latitude
      t.float :longitude
      t.timestamps
    end
  end
end

6 - tested using rails console:

thu-mbp:rails50 thu$ rails c
PlacesRunning via Spring preloader in process 47864
Loading development environment (Rails 5.0.0.beta1)
2.3.0 :001 > Place.all
  Place Load (1.4ms)  SELECT "places".* FROM "places"
 => #<ActiveRecord::Relation []>
2.3.0 :002 > p = Place.new(address: '792 Gladstone Ave, Ottawa, Ontario, K1R6X2')
 => #<Place id: nil, address: "792 Gladstone Ave, Ottawa, Ontario, K1R6X2", latitude: nil, longitude: nil, created_at: nil, updated_at: nil>
2.3.0 :003 > p.save!
   (0.2ms)  begin transaction
  SQL (1.3ms)  INSERT INTO "places" ("address", "latitude", "longitude", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["address", "792 Gladstone Ave, Ottawa, Ontario, K1R6X2"], ["latitude", 45.4061245], ["longitude", -75.70760589999999], ["created_at", 2016-01-17 15:05:24 UTC], ["updated_at", 2016-01-17 15:05:24 UTC]]
   (0.9ms)  commit transaction
 => true
2.3.0 :004 > Place.first
  Place Load (1.5ms)  SELECT  "places".* FROM "places" ORDER BY "places"."id" ASC LIMIT ?  [["LIMIT", 1]]
 => #<Place id: 1, address: "792 Gladstone Ave, Ottawa, Ontario, K1R6X2", latitude: 45.4061245, longitude: -75.70760589999999, created_at: "2016-01-17 15:05:24", updated_at: "2016-01-17 15:05:24">
2.3.0 :005 >

@simzen85
Copy link
Author

@TrangPham can you try to create a view and access .location from there (refer to my first post) ?

The error is that ActionDispatch::Request doesn't understand .location (since Geocoder::Request wasn't initialized), so your steps to reproduce weren't sufficient

@TrangPham
Copy link
Contributor

Ah sorry I did not fully understand the use case.

For your use case you have a Request object which includes Geocoder::Request? And you're passing this to the view in your controller? Is that the use case you have?

@simzen85
Copy link
Author

@TrangPham
"a Request object which includes Geocoder::Request? And you're passing this to the view in your controller" yes, and this is the default behaviour in Geocoder with previous Rails.

In more detail, you can try to insert this line in config/application.rb

config.middleware.use Geocoder::Request

There is an error when Geocoder::Request can't be initialized.

@TrangPham
Copy link
Contributor

Cannot reproduce issue for request.location:

Additional steps to reproduce:
1 - add into geocoder config

Geocoder::Configuration.ip_lookup = :freegeoip

2 - create controller

class PlaceController < ApplicationController
    def index
        @request = Rack::Request.new({'REMOTE_ADDR' => '74.200.247.59'})
        @location = @request.location
    end
end

3 - add in view

Request.location
<%= @request.location %>

Location
<%= @location %>

4 - add in routes

resources :place

5 - run rails server and go to localhost:####/place

Request.location #<Geocoder::Result::Freegeoip:0x007fa2e99f21d8> Location #<Geocoder::Result::Freegeoip:0x007fa2e99f21d8>

@TrangPham
Copy link
Contributor

@simzen85

Your issue is that you're not using Rack::Request. Geocoder::Request is included in previous versions of geocoder but only ever for Rack::Request. https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/request.rb#L81

If I change my controller to use ActionDispatch::Request then it does give me the error you're seeing:

undefined method `location' for #<ActionDispatch::Request:0x007fa2f131f538>

Since geocoder does not include Geocoder::Request onto ActionDispatch::Request , if you want to continue using ActionDispatch::Request and get .location then please include Geocoder::Request in your ActionDispatch::Request.

@alexreisner
Copy link
Owner

This does appear to be a problem with the way Geocoder is being loaded. I can reproduce by creating a clean 5.0.0beta1 app and calling request.location from a controller action.

@alexreisner alexreisner reopened this Jan 18, 2016
@simzen85
Copy link
Author

@TrangPham tks for your suggestion, I can fix the problem by inserting this line in config/application.rb

ActionDispatch::Request.send :include, Geocoder::Request

but maybe there is a long-term fix for it ? Thanks.

@TrangPham
Copy link
Contributor

@alexreisner Could you post what your controller action looks like? I want to understand why I was not able to reproduce. Was I using request.location incorrectly?

@alexreisner
Copy link
Owner

@TrangPham It's just a one-liner:

def test
  @loc = request.location
end

I think the problem was that you created a @request object, rather than working with the one in the controller.

@TrangPham
Copy link
Contributor

Issue was caused by this change: rails/rails@529136d (September 4th 2015) ActionDispatch::Request stopped inheriting from Rack::Request

Note that rails/rails@319ae46 (Jan 27, 2009) ActionController::Request was changed to ActionDispatch::Request. This happened since rails 3 and geocoder does not actively support rails 2. So we can safely do the following:

Beginner task:

Include Geocoder::Request in ActionDispatch::Request instead of Rack::Request https://github.com/alexreisner/geocoder/blob/master/lib/geocoder/request.rb#L81L83

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

No branches or pull requests

3 participants