在编写基于位置的状态时外包地理编码器位置

时间:2014-01-07 09:37:58

标签: javascript ruby-on-rails rails-geocoder

我目前能够在我的应用中写入状态,在创建新状态时将获取的用户位置(html5 javascript - > geocoder)保存到状态数据库中。现在我想将位置外包到一个新模型中,并在我的状态模型中与它相关联。就像是 状态模型:内容,Location_id ....位置模型:Lat,Lng,Address。我的问题是保存状态的表单。我将在一个中使用两种形式。一个用于状态,另一个用于保存位置。如何将正确的location_id与特定状态相关联?因为location_id仅在保存整个状态后生成。 有任何想法吗?也许有些异步的东西?我真的不知道如何解决这个问题。我将衷心感谢您的帮助 :) 祝大家新年快乐:D

1 个答案:

答案 0 :(得分:0)

在状态控制器中:

class StatusController < ApplicationController

  [..]

  def create
    @status = Status.new(params[:status])
    @location = Location.new(whatever parameters you want : nested params, or anything else)
    status = @status.save
    if status && @location.save
       redirect_to @status, notice: 'Sucessfully created.'
    elsif status
       @status.destroy
       redirect_to new_status_path, alert: 'Something went wrong with your location.'
    else
       redirect_to new_status_path, alert: 'Something went wrong'
    end

  end

  [..]

end