在Rails中使用Geocoder宝石时出现“undefined method `address=' for”错误

3
我正在开发一个应用程序,可以存储供应商详细信息以及纬度/经度信息。为了使其能够进行地理编码,我使用了geocoder gem,同时在postgis中使用它。我已经完全遵循了geocoder的文档。但是,在创建任何新供应商时,我遇到了以下错误。
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

我在代码中没有使用地址字段,但是每当我尝试从控制台或表单中创建新的供应商时,它都会抛出这个错误。我尝试了重启服务器,但无济于事。我是Rails的新手,非常希望能得到详细说明的帮助。如果您需要更多信息,请让我知道。


你可以尝试将你的“full_path”方法重命名为“address”。 - Joeyjoejoe
1个回答

3

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

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

我会运行迁移以在您的vendors表中添加address列。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接