gpt4 book ai didi

ruby-on-rails - "undefined method ` 地址= ' for"在 rails 中使用地理编码器 gem 时出错

转载 作者:行者123 更新时间:2023-12-05 05:23:14 28 4
gpt4 key购买 nike

我正在开发一个可以存储供应商详细信息以及纬度/经度信息的应用程序。为了使其可地理编码,我在 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

我的供应商.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 新手,非常感谢您提供详尽描述的帮助。如果您需要更多信息,请告诉我

最佳答案

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

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

我将运行迁移以将 address 列添加到您的 vendors

关于ruby-on-rails - "undefined method ` 地址= ' for"在 rails 中使用地理编码器 gem 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38590814/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com