NoMethodError in Devise::RegistrationsController#create /undefined method `encrypted_password=' for #<User:0x459b2b0>
Asked Answered
T

1

1

I am using omniauth and devise for login/sessions/signup

facebook login...

I get the error anytime I try to sign up, login, or etc.

I guess some other things don t work in my sessions and callbacks.

I tried adding encrypted_password in the migrations but still not working. Here is the migration:

class AddColumnsToUsers < ActiveRecord::Migration
  def change
    add_column :users, :provider, :string
    add_column :users, :uid, :string
    add_column :users, :encrypted_password, :string
  end
end

Here is my full trace :

activemodel (4.1.8) lib/active_model/attribute_methods.rb:435:in  `method_missing'
activerecord (4.1.8) lib/active_record/attribute_methods.rb:213:in `method_missing'
devise (3.4.1) lib/devise/models/database_authenticatable.rb:42:in `password='
activerecord (4.1.8) lib/active_record/attribute_assignment.rb:45:in `public_send'
activerecord (4.1.8) lib/active_record/attribute_assignment.rb:45:in `_assign_attribute'
activerecord (4.1.8) lib/active_record/attribute_assignment.rb:32:in `block in assign_attributes'
activerecord (4.1.8) lib/active_record/attribute_assignment.rb:26:in `each'
activerecord (4.1.8) lib/active_record/attribute_assignment.rb:26:in `assign_attributes'
activerecord (4.1.8) lib/active_record/core.rb:453:in `init_attributes'
activerecord (4.1.8) lib/active_record/core.rb:196:in `initialize'
devise (3.4.1) lib/devise/models/confirmable.rb:47:in `initialize'
activerecord (4.1.8) lib/active_record/inheritance.rb:30:in `new'
activerecord (4.1.8) lib/active_record/inheritance.rb:30:in `new'
devise (3.4.1) lib/devise/models/registerable.rb:20:in `new_with_session'
devise (3.4.1) app/controllers/devise/registrations_controller.rb:105:in `build_resource'
devise (3.4.1) app/controllers/devise/registrations_controller.rb:17:in `create'
actionpack (4.1.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:189:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:229:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in     halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.8) lib/active_support/callbacks.rb:86:in `run_callbacks'
actionpack (4.1.8) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `block in instrument'
activesupport (4.1.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.1.8) lib/active_support/notifications.rb:159:in `instrument'
actionpack (4.1.8) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.1.8) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.1.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.1.8) lib/abstract_controller/base.rb:136:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/mapper.rb:45:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
omniauth (1.2.2) lib/omniauth/strategy.rb:186:in `call!'
omniauth (1.2.2) lib/omniauth/strategy.rb:164:in `call'
omniauth (1.2.2) lib/omniauth/builder.rb:59:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.5.2) lib/rack/etag.rb:23:in `call'
rack (1.5.2) lib/rack/conditionalget.rb:35:in `call'
rack (1.5.2) lib/rack/head.rb:11:in `call'
actionpack (4.1.8) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
 actionpack (4.1.8) lib/action_dispatch/middleware/flash.rb:254:in `call'
 rack (1.5.2) lib/rack/session/abstract/id.rb:225:in `context'
 rack (1.5.2) lib/rack/session/abstract/id.rb:220:in `call'
 actionpack (4.1.8) lib/action_dispatch/middleware/cookies.rb:560:in `call'
 activerecord (4.1.8) lib/active_record/query_cache.rb:36:in `call'
 activerecord (4.1.8)    lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
 activerecord (4.1.8) lib/active_record/migration.rb:380:in `call'
 actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
 activesupport (4.1.8) lib/active_support/callbacks.rb:82:in `run_callbacks'
 actionpack (4.1.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
 actionpack (4.1.8) lib/action_dispatch/middleware/reloader.rb:73:in `call'
 actionpack (4.1.8) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
 actionpack (4.1.8) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
 actionpack (4.1.8) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
 railties (4.1.8) lib/rails/rack/logger.rb:38:in `call_app'
 railties (4.1.8) lib/rails/rack/logger.rb:20:in `block in call'
 activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `block in tagged'
 activesupport (4.1.8) lib/active_support/tagged_logging.rb:26:in `tagged'
 activesupport (4.1.8) lib/active_support/tagged_logging.rb:68:in `tagged'
 railties (4.1.8) lib/rails/rack/logger.rb:20:in `call'
 actionpack (4.1.8) lib/action_dispatch/middleware/request_id.rb:21:in `call'
 rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
 rack (1.5.2) lib/rack/runtime.rb:17:in `call'
 activesupport (4.1.8)     lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
 rack (1.5.2) lib/rack/lock.rb:17:in `call'
 actionpack (4.1.8) lib/action_dispatch/middleware/static.rb:84:in `call'
 rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
 railties (4.1.8) lib/rails/engine.rb:514:in `call'
 railties (4.1.8) lib/rails/application.rb:144:in `call'
 rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
 C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
 C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
 C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block   in start_thread'
Request

Parameters:

{"utf8"=>"✓",
"authenticity_token"=>"dWhoeyx03zYPBAPxqRFfzqyEHBBpyjZmlwiQEsrHXRc=",
"user"=>{"email"=>"[email protected]",
"password"=>"[FILTERED]",
"password_confirmation"=>"[FILTERED]"},
"commit"=>"Sign up"}

Here is my userController :

class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy, :finish_signup]

def index
@users = User.all
end 

# GET /users/:id.:format
def show
# authorize! :read, @user
end

# GET /users/:id/edit
def edit
# authorize! :update, @user
end
def new
@user = User.new
end
# PATCH/PUT /users/:id.:format
def update
# authorize! :update, @user
respond_to do |format|
  if @user.update(user_params)
    sign_in(@user == current_user ? @user : current_user, :bypass => true)
    format.html { redirect_to @user, notice: 'Your profile was successfully updated.' }
    format.json { head :no_content }
  else
    format.html { render action: 'edit' }
    format.json { render json: @user.errors, status: :unprocessable_entity }
  end
  end
  end

  # GET/PATCH /users/:id/finish_signup
  def finish_signup
  # authorize! :update, @user 
  if request.patch? && params[:user] #&& params[:user][:email]
  if @user.update(user_params)
    @user.skip_reconfirmation!
    sign_in(@user, :bypass => true)
    redirect_to @user, notice: 'Your profile was successfully updated.'
  else
    @show_errors = true
    end
  end
  end

 # DELETE /users/:id.:format
 def destroy
 # authorize! :delete, @user
  @user.destroy
  respond_to do |format|
  format.html { redirect_to root_url }
  format.json { head :no_content }
  end
  end
  def set_user
  @user = User.find(params[:id])
  end
  private
  def user_params
  accessible = [ :name, :email ] # extend with your own params
  accessible << [ :password, :password_confirmation ] unless params[:user]  [:password].blank?
  params.require(:user).permit(:database_authenticatable, :registerable,   :confirmable,:recoverable, :rememberable, :trackable, :validatable,      :omniauthable,:encrypted_password)
end
end

Here is my omniAuth callback controller:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def self.provides_callback_for(provider)
class_eval %Q{
  def #{provider}
    @user = User.find_for_oauth(env["omniauth.auth"], current_user)

    if @user.persisted?
      sign_in_and_redirect @user, event: :authentication
      set_flash_message(:notice, :success, kind: "#{provider}".capitalize)  if is_navigational_format?
    else
      session["devise.#{provider}_data"] = env["omniauth.auth"]
      redirect_to new_user_registration_url
    end
    end
    }
   end

   [:twitter, :facebook, :linked_in].each do |provider|
   provides_callback_for provider
   end

   def after_sign_in_path_for(resource)
   if resource.email_verified?
   super resource
   else
   finish_signup_path(resource)
   end
   end
   end

And my user model:

  class User < ActiveRecord::Base
  TEMP_EMAIL_PREFIX = 'change@me'
  TEMP_EMAIL_REGEX = /\Achange@me/
  # Include default devise modules. Others available are:
  # :lockable, :timeoutable
  devise :database_authenticatable, :registerable, :confirmable,
  :recoverable, :rememberable, :trackable, :validatable, :omniauthable
  validates_format_of :email, :without => TEMP_EMAIL_REGEX, on: :update
  def self.find_for_oauth(auth, signed_in_resource = nil)
  # Get the identity and user if they exist
  identity = Identity.find_for_oauth(auth)
   # If a signed_in_resource is provided it always overrides the existing    user
   # to prevent the identity being locked with accidentally created accounts.
   # Note that this may leave zombie accounts (with no associated identity)  which
   # can be cleaned up at a later date.
   user = signed_in_resource ? signed_in_resource : identity.user

   # Create the user if needed
   if user.nil?

  # Get the existing user by email if the provider gives us a verified email.
  # If no verified email was provided we assign a temporary email and ask the
  # user to verify it on the next step via UsersController.finish_signup
  email_is_verified = auth.info.email && (auth.info.verified || auth.info.verified_email)
  email = auth.info.email if email_is_verified
  user = User.where(:email => email).first if email

  # Create the user if it's a new registration
  if user.nil?
    user = User.new(
      name: auth.extra.raw_info.name,
      #username: auth.info.nickname || auth.uid,
      email: email ? email : "#{TEMP_EMAIL_PREFIX}-#{auth.uid}-#{auth.provider}.com",
      password: Devise.friendly_token[0,20]
    )
    user.skip_confirmation!
    user.save!
  end
end

# Associate the identity with the user if needed
if identity.user != user
  identity.user = user
  identity.save!
end
user
end

def email_verified?
self.email && self.email !~ TEMP_EMAIL_REGEX
 end
end
Tilburg answered 15/5, 2015 at 22:38 Comment(6)
Why is this code so hard to read?Daye
I guess bad indentingTilburg
possible duplicate of Get NoMethodError in Devise::RegistrationsController#createVinosity
Did the migration run successfully? You typed add_columWenzel
Ok I see what you are talking aboutTilburg
@MikeLyons - it's fundamental copy-paste problemNalor
F
0

Please run bundle exec rake db:reset

Fluorite answered 22/7, 2015 at 16:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.