glennm
glennm

Reputation: 335

Using Geocode Ruby Gem for User Address input

I've using the Geocode Ruby Gem to convert the users input of zip/country into a nicely formated location (eg. Los Angeles, CA, USA). I also want to use the geocoding in the future (just in case you wonder why I'm going to so much trouble to pretty up the format

I migrated a column "address" into the user database. now I'm getting stuck with the geocoding and don't know if it is because I didn't create a locations folder in the database rather than migrate the column with the user database.

I'm getting a routing error: undefined method `geocoded_by' for #

I'm new at all this so I'm sorry if I'm not making any sense.

class Location < ActiveRecord::Base
  attr_accessible :address, :latitude, :longitude

 geocoded_by :full_address
 after_validation :geocode, :if => :address_changed? 
end

Here's my controller:

def full_address 
  @user = User.find(params[:address])
  Geocode.serach("@user")
end 

Changing my location.rb to the following:

class Location < ActiveRecord::Base
extend ::Geocoder::Model::ActiveRecord 

  attr_accessible :address, :latitude, :longitude

  geocoded_by :address_for_geocode 
  reverse_geocoded_by :latitude, :longitude 
end

Results in the following error:

Routing Error

uninitialized constant Location::Geocoder

Try running rake routes for more information on available routes. 

Upvotes: 0

Views: 3740

Answers (2)

Milan Jaric
Milan Jaric

Reputation: 5646

You are getting Routing Error since you didn't understand what to change and how in code.

First

class Location < ActiveRecord::Base

  attr_accessible :address, :latitude, :longitude
  # comment this out! => Geocoder.search("New York, NY, USA")
end

in your controller method, just try to execute

def full_address 
  @user = User.find(params[:address])
  render :text => Geocoder.search("New York, NY, USA").inspect.to_s
end 

you will get either error (if gem is not loaded in your app) or some result.

Lets clear this out before we try anything else.

Run aaplication again and execute that route for full_address controller action

BTW, you can try all in rails console

/path/to/project# rails c

irb> Geocoder.search("New York, NY, USA")

Update

try to include geocoder in Loacation model

class Location < ActiveRecord::Base
  extend ::Geocoder::Model::ActiveRecord

  geocoded_by :address_for_geocode
  reverse_geocoded_by :latitude, :longitude
end

I just checked source code in their git repository, they are not extending ActivRecord::Base so you have to do it manualy!!!

Upvotes: 1

dompuAmanat Fadilla
dompuAmanat Fadilla

Reputation: 1082

Please check your controller

def full_address 
  @user = User.find(params[:address])
  Geocode.serach("@user")
end

it should be

def full_address 
  @user = User.find(params[:address])
  Geocode.search("@user")
end 

Upvotes: 0

Related Questions