“未定义的方法`地址='为”错误使用Geocoder gem in rails
问题描述:
我正在研究可以存储供应商详细信息以及纬度/经度信息的应用程序。为了使它成为geocodable,我在postgis上使用geocoder gem。我遵循了geocoder的完整文档。然而在创建任何新的供应商,我得到以下错误“未定义的方法`地址='为”错误使用Geocoder gem in rails
NoMethodError in VendorsController#create
undefined method `address=' for #<Vendor:0x007faca1729a20> Did you
mean?addressline2=
我轨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
我没有地址在代码中任何地方都可以使用ess字段。然而,每当我尝试从控制台和表单创建新的供应商时,它都会抛出这个错误。我试过重新启动服务器,这是徒劳的尝试。我是轨道新手,真的很感谢一些帮助,详细说明。让我知道如果你需要更多的信息
答
除非你指定,否则,地理编码器期望有一个:address
列以放置反向地理编码的结果。您可以更改默认像这样:
reverse_geocoded_by :latitude, :longitude, :address => :location
after_validation :reverse_geocode
我会跑迁移到address
列添加到您的vendors
表
您可以尝试重命名你的“full_path”法“地址” – Joeyjoejoe