Question

I'm currently working on implementing Geokit into my project. I want to take the info that a user enters onto the create website, geocode the address, and create the entry with the new latitude and longitude created. However, lat or long won't be entered by the user because our geokit will do it for them. Overall, when the user presses the "Create" button, our controller should take all the info (assuming it's been validated) geocode the given address, and use the generated lat and long to create the new entry. I have some general idea how to but not exactly sure if directly changing the controller is the right method to do so. I've included my controller code for visual help. Many thanks in advance!

Overall question - Where do you put the method for geocoding an address and automatically using the generated lat and long to create the rest of the data entry?

class PocsController < ApplicationController

# GET /pocs # GET /pocs.json def index @pocs = Poc.all @pocs = Poc.find(:all) @pocs.sort! { |a,b| a.name.downcase <=> b.name.downcase }

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @pocs }
end

end

# GET /pocs/1 # GET /pocs/1.json def show @poc = Poc.find(params[:id]) @pocs = Poc.all

respond_to do |format|
  format.html # show.html.erb
  format.json { render json: @poc }
end

end

# GET /pocs/new # GET /pocs/new.json def new @poc = Poc.new

respond_to do |format|
  format.html # new.html.erb
  format.json { render json: @poc }
end

end

# GET /pocs/1/edit def edit @poc = Poc.find(params[:id]) end

# POST /pocs # POST /pocs.json def create @poc = Poc.new(params[:poc])

respond_to do |format|
  if @poc.save
    format.html { redirect_to @poc, notice: 'Poc was successfully created.' }
    format.json { render json: @poc, status: :created, location: @poc }
  else
    format.html { render action: "new" }
    format.json { render json: @poc.errors, status: :unprocessable_entity }
  end
end

end

# PUT /pocs/1 # PUT /pocs/1.json def update @poc = Poc.find(params[:id])

respond_to do |format|
  if @poc.update_attributes(params[:poc])
    format.html { redirect_to @poc, notice: 'Poc was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: "edit" }
    format.json { render json: @poc.errors, status: :unprocessable_entity }
  end
end

end

# DELETE /pocs/1 # DELETE /pocs/1.json def destroy @poc = Poc.find(params[:id]) @poc.destroy

respond_to do |format|
  format.html { redirect_to pocs_url }
  format.json { head :no_content }
end

end end

Was it helpful?

Solution

Based on your comment on the question itself, it sounds like you're looking for help figuring out where to plug in the geocoding. According to the docs (specifically the 'Auto Geocoding' section), Geokit can handle this for you. So, your app/model/Poc.rb should just need:

class Poc < ActiveRecord::Base
  acts_as_mappable :auto_geocode=>true
end

Note you'll need fields named address, lat, and lng (or will need to specify your own overrides).

Since this defines a before_validation_on_create, it will run automatically whenever a new Poc is created. That won't cover the case of updating the lat/lng fields if the address field changes... but by researching ActiveModel callbacks, I bet you can figure out how to handle that, if it's needed. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top