-1

I'm trying to get Geocoder working in my app. I am trying to use a user's zip code to geocode their location and I've run into this error upon submitting a new sign-up:

NoMethodError in Devise::RegistrationsController#create
undefined method `latitude='

I'm using Devise for authentication, here's my sanitation of the :zip attribute in case it's relevant. Also below is my User sign up form.

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :configure_permitted_parameters, if: :devise_controller?

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:zip, :email, :password, :password_confirmation) }
end

end

Here is my relevant User model:

class User < ActiveRecord::Base

validates :zip, presence: true
geocoded_by :zip
after_validation :geocode

end

My sign-up form:

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %>
  <%= f.email_field :email, :autofocus => true %></div>

  <div><%= f.label :zip %>
  <%= f.text_field :zip %> </div>

  <div><%= f.label :password %>
  <%= f.password_field :password %></div>

  <div><%= f.label :password_confirmation %>
  <%= f.password_field :password_confirmation %></div>

  <div><%= f.submit "Sign up" %></div>
<% end %>

<%= render "devise/shared/links" %>

Any thoughts on why I'm getting this error? Do I need to include a field in my sign-up form for lat and long? Thanks in advance!

EDIT

Here's the schema for my User table:

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|

      t.integer :zip
      t.float :latitude
      t.float :longitude
      ## Database authenticatable
      t.string :email,              :null => false, :default => ""
      t.string :encrypted_password, :null => false, :default => ""
      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at
      ## Rememberable
      t.datetime :remember_created_at
      ## Trackable
      t.integer  :sign_in_count, :default => 0
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip
   end
  end
end
settheline
  • 3,333
  • 8
  • 33
  • 65
  • Paste up your schema for the users table (in the db/schema.rb file). The Geocoder gem will try to update these columns, which probably don't exist yet. You can use a migration to add them. – Aaron Breckenridge Oct 17 '13 at 23:56
  • Added the schema. I recreated the DB before I ran this migration. – settheline Oct 18 '13 at 01:08

3 Answers3

2

Have you added the columns latitude:float longitude:float into your user migration?, after that run rake db:migrate and see if that helps. A nice tutorial of Geocoder can be found here

torresomar
  • 2,219
  • 2
  • 16
  • 28
  • Yep, added both float columns and migrated those changes to the DB. – settheline Oct 18 '13 at 01:02
  • Hi, I reproduced a similar app as yours in order to better understand the error. I installed Devise and Geocoder, created individual migrations for latitude,longitude and zip in order to add those columns to the user. I got it working getting the correct lat & long using zip to geocode, example "....|ZIP | Longitude | Latitude " "....|70112|-90.0770127|29.9595769" What version of Rails and Devise are you using? – torresomar Oct 18 '13 at 05:49
  • rails 3.2.13 and devise 3.0.3 – settheline Oct 18 '13 at 16:49
  • So I just went ahead and dropped the db, re-created, and re-migrated. It's working now, I'm not sure why it didn't migrate correctly the first time, but now it's working. Thanks! – settheline Oct 18 '13 at 16:56
1

it is always a good idea to include the whole stacktrace as there is a lot of relevant information in there. have a look at this blog post if you don't know how to get to it: http://nofail.de/2013/10/debugging-rails-applications-in-development/

i assume that you did not run the geocoder migrations or forgot to setup something else that the geocoder after_validation :geocode hook is calling.

phoet
  • 18,688
  • 4
  • 46
  • 74
  • Cool I will check out the blog, I'm newish to RoR so I appreciate the tip. I followed the Railscast on geocoder, but I definitely could've done something wrong. What exactly do you mean by the geocoder migrations? – settheline Oct 17 '13 at 22:51
  • i mean the same thing that @Omar mentions in his answer. use a database frontend and have a look at the columns of your db. is there a `latitude` column? – phoet Oct 18 '13 at 06:45
1
class Location < ActiveRecord::Base
belongs_to :user

geocoded_by :address
after_validation :geocode, :if => :address_changed?
end

Rather than geocoded_by :zip, you may want to try geocoded_by :address . Geocoder recognizes zip codes in the address field. Let me know if this works

wyattwade
  • 38
  • 4