Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 3:创建问题后,将acts作为可安装_Ruby On Rails_Ruby On Rails 3_Gmaps4rails - Fatal编程技术网

Ruby on rails Rails 3:创建问题后,将acts作为可安装

Ruby on rails Rails 3:创建问题后,将acts作为可安装,ruby-on-rails,ruby-on-rails-3,gmaps4rails,Ruby On Rails,Ruby On Rails 3,Gmaps4rails,我构建了一个应用程序,它可以创建一个用户模型,并使用模型中的after_create回调方法自动创建一个配置文件模型和一个位置(称为living)模型。模型配置文件属于用户,用户有一个配置文件,模型位置属于配置文件,配置文件有一个位置 称为位置的模型,使用gem地理编码器和客户端机器的当前ip地址,拉出用户的当前位置,然后将地址、纬度和经度保存在同一模型中 一切都运行得很好,除了当我尝试添加到位置模型时,作为可安装,然后在注册过程中,相同的模型不会自动创建,这会阻止创建数据库列地址,显然,这会导

我构建了一个应用程序,它可以创建一个用户模型,并使用模型中的after_create回调方法自动创建一个配置文件模型和一个位置(称为living)模型。模型配置文件属于用户,用户有一个配置文件,模型位置属于配置文件,配置文件有一个位置

称为位置的模型,使用gem地理编码器和客户端机器的当前ip地址,拉出用户的当前位置,然后将地址、纬度和经度保存在同一模型中

一切都运行得很好,除了当我尝试添加到位置模型时,作为可安装,然后在注册过程中,相同的模型不会自动创建,这会阻止创建数据库列地址,显然,这会导致GmapForails错误

如何在概要文件模型中使用after_create回调,然后自动创建位置模型及其列(经度、纬度、地址、ip_地址),同时使用after the act_as_gmappable

我认为这是一个与之前调用哪个方法有关的问题:after_create或gmapsforrails一个……这是因为,如果我从我的位置模型中删除对acts_as_gmappable的调用,注册用户,然后在位置模型中返回acts_as_gmappable,一切正常,谷歌地图就会显示出来

感谢所有能以某种方式突出我思想的人

(1) 剖面模型

class Profile < ActiveRecord::Base

  # Setup model relationships (has_many require plural)
  belongs_to :user
  has_one :living

  after_create :create_living

  accepts_nested_attributes_for :living

  # Setup accessible (or protected) attributes for your model
  attr_accessible :registration_ip, :registration_timezone, :registration_address, :registration_address_components, :latitude, :longitude, :living_attributes

  # Setup Geocoder routine
  geocoded_by :registration_ip
  after_validation :geocode

  reverse_geocoded_by :latitude, :longitude do |obj,results|
    if geo = results.first
      obj.registration_address_components  = ActiveSupport::JSON.encode(geo.address_components)
      obj.registration_address  = geo.address
    end
  end

  after_validation :reverse_geocode

end
类配置文件
(2) 生活模式

class Living < ActiveRecord::Base

  # Setup model relationships
  belongs_to :profile

  # Setup accessible (or protected) attributes for your model
  attr_accessible :current_ip, :current_timezone, :current_address, :current_address_components,     :latitude, :longitude, :gmaps

  geocoded_by :current_ip
  after_validation :geocode, :if => :current_ip_changed?

  reverse_geocoded_by :latitude, :longitude do |obj,results|
    if geo = results.first
      obj.current_address_components  = ActiveSupport::JSON.encode(geo.address_components)
      obj.current_address  = geo.address
    end
  end

  after_validation :reverse_geocode, :if => :current_ip_changed?


  acts_as_gmappable :lat => 'glat', :lng => 'glng', :check_process => :prevent_geocoding,
                    :address => "current_address", :normalized_address => "current_address",
                    :msg => "Sorry, not even Google could figure out where that is"

  def prevent_geocoding
    current_address.blank? || (!latitude.blank? && !longitude.blank?)
  end
end
class-Living:当前\u ip\u是否已更改?
反向地理编码方式:纬度、经度do | obj,结果|
如果geo=results.first
obj.current\u address\u components=ActiveSupport::JSON.encode(geo.address\u components)
obj.current_address=geo.address
结束
结束
验证后:反向地理编码,:if=>:当前\u ip\u更改?
按以下方式操作:lat=>'glat',:lng=>'glng',:检查流程=>:防止地理编码,
:address=>“当前地址”,:normalized\u address=>“当前地址”,
:msg=>“对不起,即使是谷歌也找不出它在哪里”
def防止地理编码
当前|u地址。空白?||(!latitude.blank?&!longitude.blank?)
结束
结束
请注意,配置文件的“创建前”在用户模型中。 使用此系统,每次用户注册时,应用程序都会自动在用户表中创建一条用户记录,在配置文件表中创建一条配置文件记录,在生活记录中创建一条生活记录。 除了我在living model中添加可安装的acts______________________________。在这一点上,living model表中的记录不会更自动地创建,而且显然google maps不会工作,因为它不会在living model表中找到地址(living model将为零)

我在想,可能是因为模型中使用了before_create方法,为了给living添加一个映射,我需要创建另一个属于living的模型,比如说gmapliving,但在那一点上,我不知道如何访问living model表中为地址保存的记录

请给我一些建议

谢谢
Dinuz

因为您不需要从gmaps4rails进行地理编码,所以添加此选项:

:process_geocoding => false

这里解释了一切:

因为您不需要从gmaps4rails进行地理编码,所以添加此选项:

:process_geocoding => false

这里解释了一切:

读起来有点混乱,我想所有的信息都不是必需的。我猜
gmaps4rails
正在尝试地理编码,但没有成功。您是否尝试过这样做:`def prevent_geocoding;新纪录当前|u地址。空白?||(!latitude.blank?&!longitude.blank?);end`既然你使用geocoder,为什么你需要gmaps4rails来进行地理编码?我想要地图……就这些!我想在同一张地图上显示注册位置和当前位置。我不想使用gmaps4rails进行地理编码,我想使用它允许我显示的地图。我不想要求gmaps4rails到geocod,而只是使用geocoder提供的geocod结果。通过这种方式,我可以使用gmaps4rails访问地图。请注意,我已经有了lat和long using geocoder,我想在geocoder完成自己的任务后使用它(这样,我就不需要gmaps4rails再次进行地理编码),并且能够访问gmaps4rails内置的地图功能。阅读起来有点混乱,我想所有信息都不是必需的。我猜
gmaps4rails
正在尝试地理编码,但没有成功。您是否尝试过这样做:`def prevent_geocoding;新纪录当前|u地址。空白?||(!latitude.blank?&!longitude.blank?);end`既然你使用geocoder,为什么你需要gmaps4rails来进行地理编码?我想要地图……就这些!我想展示一下