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
Comments
Yes, could you give us more information on the bug you are seeing? Specifically:
Thanks |
@TrangPham log App 4253 stdout: NoMethodError - undefined method |
@TrangPham were you able to reproduce this or do you need any other info from me ? Tks |
@simzen85 I cannot reproduce the issue. I believe there is something wrong specifically your configuration and/or environment. Steps to try to reproduce: 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 > |
@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 |
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? |
@TrangPham 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. |
Cannot reproduce issue for request.location: Additional steps to reproduce: 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
|
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
Since geocoder does not include Geocoder::Request onto ActionDispatch::Request , if you want to continue using ActionDispatch::Request and get |
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 |
@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. |
@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? |
@TrangPham It's just a one-liner:
I think the problem was that you created a |
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 |
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
The text was updated successfully, but these errors were encountered: