"未定义的方法`地址='对于"在rails中使用geocoder gem的错误

时间:2016-07-26 13:09:03

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

我正在开发一个可以存储供应商详细信息以及lat / lon信息的应用程序。为了使其可地定位,我在postgis上使用时使用了geocoder gem。我已经按照地理编码器的完整文档。但是,在创建任何新供应商时,我收到以下错误

NoMethodError in VendorsController#create

undefined method `address=' for #<Vendor:0x007faca1729a20> Did you 
mean?addressline2=

我的rails vendors_controller.rb

class VendorsController < ApplicationController
  before_action :set_vendor, only: [:show, :edit, :update, :destroy]


  def index
    @vendors = Vendor.all.limit(20)
  end

  def new
    @vendor = Vendor.new
  end

  def create
    @vendor = Vendor.new(vendor_params)
    respond_to do |format|
      if @vendor.save
        format.html { redirect_to @vendor, notice: 'Vendor was 
      successfully created.' }
      format.json { render :show, status: :created, location: @vendor }
  else
    format.html { render :new }
    format.json { render json: @vendor.errors, status: 
   :unprocessable_entity }
  end
  end
  end

  def update
    respond_to do |format|
      if @vendor.update(vendor_params)
        format.html { redirect_to @vendor, notice: 'Vendor was 
      successfully 
    updated.' }
    format.json { render :show, status: :ok, location: @vendor }
  else
    format.html { render :edit }
    format.json { render json: @vendor.errors, status: 
   :unprocessable_entity }
  end
  end
  end


 private
def set_vendor
  @vendor = Vendor.find(params[:id])
end


def vendor_params
  params.require(:vendor).permit(:name, :email, :phone_no, :addressline1, 
  :addressline2, :landmark, 
  :city, :state, :country, :pincode, :latitude, :longitude, :status)
end
end

我的vendor.rb

  class Vendor < ActiveRecord::Base
  has_many :vendor_products
  has_many :products, through: :vendor_products


  geocoded_by :full_path
  after_validation :geocode

  reverse_geocoded_by :latitude, :longitude
  after_validation :reverse_geocode

  def full_path
    [addressline1, addressline2, landmark, city, state, country,  
    pincode].compact.join(', ')
  end
 end

我的供应商架构

create_table "vendors", force: :cascade do |t|
t.string   "name"
t.string   "email"
t.string   "phone_no"
t.string   "addressline1"
t.string   "addressline2"
t.string   "landmark"
t.string   "city"
t.string   "state"
t.string   "country"
t.float    "latitude"
t.float    "longitude"
t.boolean  "status"
t.datetime "created_at",   null: false
t.datetime "updated_at",   null: false
t.string   "pincode"
end

我根本没有在代码中的任何地方使用地址字段。然而,每当我尝试从控制台和表单创建新的供应商时,它就会抛出这个错误。我试过重启服务器,这是徒劳的尝试。我是铁杆新手,非常感谢一些详细描述的帮助。如果您需要更多信息,请告诉我

1 个答案:

答案 0 :(得分:1)

除非您另行指定,否则地理编码器预计会有一个:address列来放置反向地理编码的结果。您可以像这样更改默认值:

reverse_geocoded_by :latitude, :longitude, :address => :location
after_validation :reverse_geocode

我会运行迁移,向address

添加vendors