【问题标题】:"undefined method `address=' for" error using geocoder gem in rails在 Rails 中使用地理编码器 gem 时出现“未定义的方法 `address='for”错误
【发布时间】:2016-07-26 13:09:02
【问题描述】:

我正在开发一个可以存储供应商详细信息以及纬度/经度信息的应用程序。为了使其具有地理编码能力,我在 postgis 上使用了 geocoder gem。我遵循了地理编码器的完整文档。但是,在创建任何新供应商时,我收到以下错误

NoMethodError in VendorsController#create

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

我的 rails vendor_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”

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多