Mahesh Mesta
Mahesh Mesta

Reputation: 783

"undefined method `address=' for" error using geocoder gem in rails

I am working on an application which can store vendor details along with lat/lon information. To make it geocodable I have used geocoder gem while using it on postgis. I have followed the complete documentation of geocoder. However while creating any new vendor I get the following error

NoMethodError in VendorsController#create

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

My 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

My 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

My vendor schema

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

I do not have an address field used anywhere in the code at all. Yet it throws me this error whenever I try to create a new vendor both from the console as well as the form. I have tried restarting the server, it was a futile attempt. I am rails newbie and would really appreciate some help with an elaborate description.Let me know if you need more info

Upvotes: 3

Views: 1194

Answers (1)

Ren
Ren

Reputation: 1374

Unless you specify otherwise, geocoder expects there to be an :address column to place the results of reverse geocoding. You can change the default like so:

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

I would run a migration to add an address column to your vendors table

Upvotes: 3

Related Questions