Question

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
Was it helpful?

Solution

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

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top