Ruby on rails 未定义的方法`纬度=';地理编码器轨道

Ruby on rails 未定义的方法`纬度=';地理编码器轨道,ruby-on-rails,devise,rails-geocoder,Ruby On Rails,Devise,Rails Geocoder,我正在尝试让Geocoder在我的应用程序中工作。我试图使用用户的邮政编码对其位置进行地理编码,在提交新注册时遇到此错误: NoMethodError in Devise::RegistrationsController#create undefined method `latitude=' 我正在使用Desive进行身份验证,下面是我的:zip属性的卫生条件,以防相关。下面是我的用户注册表 class ApplicationController < ActionController::

我正在尝试让Geocoder在我的应用程序中工作。我试图使用用户的邮政编码对其位置进行地理编码,在提交新注册时遇到此错误:

NoMethodError in Devise::RegistrationsController#create
undefined method `latitude='
我正在使用Desive进行身份验证,下面是我的
:zip
属性的卫生条件,以防相关。下面是我的用户注册表

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:zip, :email, :password, :password_confirmation) }
end

end
class ApplicationController
以下是我的相关用户模型:

class User < ActiveRecord::Base

validates :zip, presence: true
geocoded_by :zip
after_validation :geocode

end
class用户
我的报名表格:

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %>
  <%= f.email_field :email, :autofocus => true %></div>

  <div><%= f.label :zip %>
  <%= f.text_field :zip %> </div>

  <div><%= f.label :password %>
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation %></div>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>
注册
资源名称:url=>注册路径(资源名称))do | f |%>
正确%>
你知道我为什么会犯这个错误吗?我是否需要在我的注册表格中包含一个字段以进行lat和long?提前谢谢

编辑

以下是我的用户表的架构:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|

      t.integer :zip
      t.float :latitude
      t.float :longitude
      ## Database authenticatable
      t.string :email,              :null => false, :default => ""
      t.string :encrypted_password, :null => false, :default => ""
      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at
      ## Rememberable
      t.datetime :remember_created_at
      ## Trackable
      t.integer  :sign_in_count, :default => 0
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip
   end
  end
end
classdeveliecCreateUsersfalse,:default=>
t、 字符串:加密密码,:null=>false,:default=>
##可恢复
t、 字符串:重置密码\u令牌
t、 日期时间:重置密码发送时间
##难忘的
t、 datetime:记住在
##可追踪
t、 整数:计数中的符号,默认值=>0
t、 日期时间:当前登录时间
t、 日期时间:上次登录时间
t、 字符串:ip中的当前\u符号\u
t、 字符串:ip中的最后\u签名\u
结束
结束
结束

最好包含整个stacktrace,因为其中包含大量相关信息。如果您不知道如何访问此博文,请查看此博文:


我假设您没有运行geocoder迁移,或者忘记设置geocoder
在_validation:geocode
hook调用后调用的其他内容。

您是否在用户迁移中添加了latitude:float longitude:float列?,然后运行rake db:migrate,看看是否有帮助。 地理编码器的一个不错的教程可以找到

类位置:地址改变了吗?
结束

您可以尝试使用
geocoded\u by:address
,而不是
geocoded\u by:zip
。地理编码器识别地址字段中的邮政编码。让我知道这是否有效

很酷,我会查看博客,我对RoR还很陌生,所以我很感激这个提示。我在geocoder上跟踪了Railscast,但我肯定会做错什么。你所说的地理编码器迁移到底是什么意思?我的意思和@Omar在回答中提到的一样。使用数据库前端并查看数据库的列。是否有
latitude
列?粘贴用户表的模式(在db/schema.rb文件中)。Geocoder gem将尝试更新这些列,这些列可能还不存在。您可以使用迁移来添加它们。已添加架构。在运行迁移之前,我重新创建了数据库。是的,添加了两个浮点列,并将这些更改迁移到了数据库中。嗨,为了更好地理解错误,我复制了一个与您类似的应用程序。我安装了designe和Geocoder,为纬度、经度和邮政编码创建了单独的迁移,以便向用户添加这些列。我使用zip-to-geocode获得正确的lat&long,例如“…| zip |经度|纬度”…..| 70112 |-90.0770127 | 29.9595769”您使用的是什么版本的Rails和Desive?Rails 3.2.13和Desive 3.0.3所以我只是继续删除数据库,重新创建并重新迁移。它现在工作了,我不知道为什么第一次没有正确迁移,但现在它工作了。谢谢
class Location < ActiveRecord::Base
belongs_to :user

geocoded_by :address
after_validation :geocode, :if => :address_changed?
end