2

我有这个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

在我的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

现在,当用户第一次创建条目时,地址被地理编码,纬度和经度保存到数据库中。

但是当用户更新地址时,经纬度将不再被地理编码,因此仍然使用第一次保存的旧经纬度。

每次更新条目时如何编写让 Rails 重新地理编码?

我不能只依赖地址,因为 geokit 中有一个错误,当我尝试根据地址显示多张地图时,只显示最后一张。

我正在使用 geokit、Gmaps、谷歌地图......

谢谢。

4

4 回答 4

2

我把它放在我的模型中:

  before_validation_on_update :geocode_address
于 2010-10-27T15:15:28.120 回答
0

如果用户更改了他们的地址,您就不能以与新地址相同的方式处理它吗?您基本上有 2 个新地址,您只需要将新创建的地址与用户帐户相关联,一切就可以正常工作了。

于 2010-10-27T14:05:23.887 回答
0

在特定操作之前执行验证的新语法是:

     before_validation :geocode_address, on: :update

或者,如果您有多个操作,

    before_validation :geocode_address, on: %i[create update]

这将确保在完成验证和保存到数据库之前,您的方法 ( geocode_address) 首先运行。

于 2018-06-20T13:51:33.710 回答
0

最好使用 Geocoder Gem https://github.com/alexreisner/geocoder

实际上,在您的模型 shop.rb 中,您需要添加以下内容,以确保每次用户更新您视图中的地址时,您的商店表中的经度和纬度字段都会更新。

宝石文件

gem 'geocoder', '~> 1.4'

您应该在 Shop 表中添加两个字段,经度和纬度,确保它们都是浮点数,如果您还没有这样做,请进行迁移。

假设这address是一个字段并且它存在于您的商店表中,并假设这location.html.erb是您商店中的一个视图,并且在该视图中您有这样的东西

<%= f.text_field :address, placeholder: "Your Shop's Address", class: "form-control", required: true, id: "shopaddress" %>

我还假设,当您创建 Shop 模型时,您添加了属性active:booleanuser:references知道商店是否处于活动状态,并且知道商店属于哪个用户。所以一个用户有很多商店。

ID shopaddress,我在这里包括以防您想将 Geocomplete gem 与 Google Maps API 与 Places Library 一起使用。但是你不需要它。

商店.rb

geocoded_by :address
# Will Update if changed
after_validation :geocode, if: :address_changed?

当然,在您的控制器中,您需要确保首先授权更新地址的人,然后运行这些方法。因此,不必重复自己。您可能想在您的商店控制器中创建类似的东西。

shop_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
于 2019-05-08T17:55:16.737 回答