问题
I have this in my shop.rb
:
def geocode_address
if !address_geo.blank?
geo=Geokit::Geocoders::MultiGeocoder.geocode(address_geo)
errors.add(:address, "Could not Geocode address") if !geo.success
self.lat, self.lng = geo.lat,geo.lng if geo.success
end
end
# Checks whether this object has been geocoded or not. Returns the truth
def geocoded?
lat? && lng?
end
And in my shops_controller.rb
:
def update
@shop = Shop.find(params[:id])
if @shop.update_attributes(params[:shop])
flash[:notice] = "Successfully saved."
redirect_to shop_path(@shop, :type => @shop.shop_type)
else
render :action => :edit
end
end
Now when the user first creates the entry, the address is geocoded, with latitude and longitude saved to the database.
But when the user updates the address, the latitude and longitude will not be geocoded anymore, and thus still using the old latitude and longitude which is the first save.
How do I write to have Rails re-geocode everytime an entry is update?
I can't depend on just the address because there is a bug in geokit that when I try to show multiple maps based on address, only the last one is shown.
I'm using geokit, Gmaps, Google Maps...
Thanks.
回答1:
I put this in my model:
before_validation_on_update :geocode_address
回答2:
If the user changes their address can't you essentially handle it the same way as a new address? You basically have 2 new addresses you just need to link the newly created address with the user's account and everything should work.
回答3:
The new syntax to perform validation before a specific action is:
before_validation :geocode_address, on: :update
or if you have more than one action,
before_validation :geocode_address, on: %i[create update]
This will ensure that before validation and saving to the database is done, your method (geocode_address
) runs first.
回答4:
Better to use Geocoder Gem https://github.com/alexreisner/geocoder
Actually in your model shop.rb you will need to to add the following to ensure that the longitude and latitude fields get updated in your shop table every time a user updates the address in your view.
Gemfile
gem 'geocoder', '~> 1.4'
You should add two fields to the Shop table, longitude and latitude, make sure they are both floats, make the migration if you have not done this already.
Assuming that address
is a field and it exists in your shop table, and assuming that location.html.erb
is a view in your shop and in that view you have something like this
<%= f.text_field :address, placeholder: "Your Shop's Address", class: "form-control", required: true, id: "shopaddress" %>
I also assuming that, when you created your Shop model you added the property active:boolean
and user:references
to know is a shop is active or not, and know to which user does the shop belong to. So one user has many shops.
The ID shopaddress, i am including here just in case you want to use Geocomplete gem with Google Maps API with the Places Library. But you do not need it there.
In shop.rb
geocoded_by :address
# Will Update if changed
after_validation :geocode, if: :address_changed?
Of course in your controller you will want to ensure that whoever is updating the address is authorized first and then run the methods. So instead of having to repeat your self. You'll probably want to create something like this in your shop controller.
In shops_controller.rb
class ShopsController < ApplicationController
# If your shop owners are creating many shops you will want to add
#your methods here as well with index. Eg. :create, :new
# In case you have a view shop page to show all people
before_action :set_shop, except: [:index]
before_action :authenticate_user!, except: [:show]
# I am assuming that you also want to update other fields in your
#shop and the address isn't the only one.
before_action :is_user_authorised, only: [:name_x, :name_y, :name_z, :location, :update]
def index
@shops = current_user.shops
end
def show
@photos = @shop.photos
@product_reviews = @shop.product_reviews
end
def name_x
end
def name_y
end
def name_z
end
def location
end
def update
new_params = shop_params
# To ensure the shop is actually published
new_params = shop_params.merge(active: true) if is_shop_ready
if @shop.update(new_params)
flash[:notice] = "Saved..."
else
flash[:alert] = "Oh oh hmm! something went wrong..."
end
redirect_back(fallback_location: request.referer)
end
private
def set_shop
@shop = Shop.find(params[:id])
end
def is_user_authorised
redirect_to root_path, alert: "You don't have permission" unless
current_user.id == @shop.user_id
end
# You can play with this here, what defines a ready shop?
def is_shop_ready
!@shop.active && !@shop.name_x.blank? &&
!@shop.name_y.blank? && !@shop.name_z.blank? &&
!@shop.address.blank?
end
# Here you are allowing the authorized user to require her shop and it's properties, so that she can update them with update method above.
# eg_summary, eg_shop_type, eg_shop_name are just additional #example properties that could have been added when you iniitially created your Shop model
def shop_params
params.require(:shop).permit(:address, :active, :eg_shop_name, :eg_shop_summary, :eg_shop_type)
end
end
来源:https://stackoverflow.com/questions/4034050/update-geocode-latitude-and-longitude-everytime-address-is-updated