Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise + OmniAuth NoMethodError UndefinedMethod Username

I'm following through Railscast #235 "Devise + OmniAuth Revised" and have run into a NoMethodError for Username whenever I try to authenticate using Twitter. I'm running Rails 3.2.

Specifically, it reads:

NoMethodError in OmniauthCallbacksController#twitter
undefined method `username' for #<User:0x007fa4744561c0>

app/models/user.rb:11:in `from_omniauth'
app/controllers/omniauth_callbacks_controller.rb:3:in `all'

I'm new to Rails, so any help would be much appreciated. Here's a look at my code:

The omniauth_callbacks_controller:

    class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def all
    user = User.from_omniauth(request.env["omniauth.auth"])
    if user.persisted?
      flash.notice = "Signed in!"
      sign_in_and_redirect user
    else
      session["devise.user_attributes"] = user.attributes
      redirect_to new_user_registration_url
    end
  end
  alias_method :twitter, :all
end

The user model:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :omniauthable,
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me, :username

  validates_presence_of :username
  validates_uniqueness_of :username

  def self.from_omniauth(auth)
    where(auth.slice(:provider, :uid)).first_or_create do |user|
      user.provider = auth.provider
      user.uid = auth.uid
      user.username = auth.info.nickname
    end
  end

  def self.new_with_session(params, session)
    if session["devise.user_attributes"]
      new(session["devise.user_attributes"], without_protection: true) do |user|
        user.attributes = params
        user.valid?
      end
    else
      super
    end
  end

  def password_required?
    super && provider.blank?
  end

  def update_with_password(params, *options)
    if encrypted_password.blank?
      update_attributes(params, *options)
    else
      super
    end
  end
end

Framework Trace:

activemodel (3.2.8) lib/active_model/attribute_methods.rb:407:in `method_missing'
activerecord (3.2.8) lib/active_record/attribute_methods.rb:149:in `method_missing'
activemodel (3.2.8) lib/active_model/errors.rb:254:in `block in add_on_blank'
activemodel (3.2.8) lib/active_model/errors.rb:253:in `each'
activemodel (3.2.8) lib/active_model/errors.rb:253:in `add_on_blank'
activemodel (3.2.8) lib/active_model/validations/presence.rb:8:in `validate'
activesupport (3.2.8) lib/active_support/callbacks.rb:310:in `_callback_before_35'
activesupport (3.2.8) lib/active_support/callbacks.rb:473:in `_run__3967236747236009763__validate__2888041296400006394__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_validate_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
activemodel (3.2.8) lib/active_model/validations.rb:227:in `run_validations!'
activemodel (3.2.8) lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
activesupport (3.2.8) lib/active_support/callbacks.rb:425:in `_run__3967236747236009763__validation__2888041296400006394__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_validation_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
activemodel (3.2.8) lib/active_model/validations/callbacks.rb:53:in `run_validations!'
activemodel (3.2.8) lib/active_model/validations.rb:194:in `valid?'
activerecord (3.2.8) lib/active_record/validations.rb:69:in `valid?'
activerecord (3.2.8) lib/active_record/validations.rb:77:in `perform_validations'
activerecord (3.2.8) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.8) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.2.8) lib/active_record/transactions.rb:241:in `block (2 levels) in save'
activerecord (3.2.8) lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.8) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.8) lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
activerecord (3.2.8) lib/active_record/transactions.rb:241:in `block in save'
activerecord (3.2.8) lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
activerecord (3.2.8) lib/active_record/transactions.rb:240:in `save'
activerecord (3.2.8) lib/active_record/persistence.rb:45:in `create'
activerecord (3.2.8) lib/active_record/relation.rb:87:in `block in create'
activerecord (3.2.8) lib/active_record/relation.rb:241:in `block in scoping'
activerecord (3.2.8) lib/active_record/scoping.rb:98:in `with_scope'
activerecord (3.2.8) lib/active_record/relation.rb:241:in `scoping'
activerecord (3.2.8) lib/active_record/relation.rb:87:in `create'
activerecord (3.2.8) lib/active_record/relation.rb:119:in `first_or_create'
actionpack (3.2.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.8) lib/active_support/callbacks.rb:436:in `_run__3199821227742357635__process_action__2555454004410598156__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.2.8) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
activerecord (3.2.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (3.2.8) lib/abstract_controller/base.rb:121:in `process'
actionpack (3.2.8) lib/abstract_controller/rendering.rb:45:in `process'
actionpack (3.2.8) lib/action_controller/metal.rb:203:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.2.8) lib/action_controller/metal.rb:246:in `block in action'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:36:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/mapper.rb:42:in `call'
journey (1.0.4) lib/journey/router.rb:68:in `block in call'
journey (1.0.4) lib/journey/router.rb:56:in `each'
journey (1.0.4) lib/journey/router.rb:56:in `call'
actionpack (3.2.8) lib/action_dispatch/routing/route_set.rb:600:in `call'
omniauth (1.1.1) lib/omniauth/strategy.rb:394:in `call_app!'
omniauth (1.1.1) lib/omniauth/strategy.rb:356:in `callback_phase'
omniauth-oauth (1.0.1) lib/omniauth/strategies/oauth.rb:58:in `callback_phase'
omniauth (1.1.1) lib/omniauth/strategy.rb:219:in `callback_call'
omniauth (1.1.1) lib/omniauth/strategy.rb:175:in `call!'
omniauth (1.1.1) lib/omniauth/strategy.rb:157:in `call'
warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
warden (1.2.1) lib/warden/manager.rb:34:in `catch'
warden (1.2.1) lib/warden/manager.rb:34:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
rack (1.4.1) lib/rack/etag.rb:23:in `call'
rack (1.4.1) lib/rack/conditionalget.rb:25:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/head.rb:14:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/flash.rb:242:in `call'
rack (1.4.1) lib/rack/session/abstract/id.rb:205:in `context'
rack (1.4.1) lib/rack/session/abstract/id.rb:200:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/cookies.rb:339:in `call'
activerecord (3.2.8) lib/active_record/query_cache.rb:64:in `call'
activerecord (3.2.8) lib/active_record/connection_adapters/abstract/connection_pool.rb:473:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `_run__635784344197953786__call__2888041296400006394__callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (3.2.8) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/reloader.rb:65:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.8) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.8) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.8) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.8) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.8) lib/rails/engine.rb:479:in `call'
railties (3.2.8) lib/rails/application.rb:223:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.8) lib/rails/rack/log_tailer.rb:17:in `call'
thin (1.4.1) lib/thin/connection.rb:80:in `block in pre_process'
thin (1.4.1) lib/thin/connection.rb:78:in `catch'
thin (1.4.1) lib/thin/connection.rb:78:in `pre_process'
thin (1.4.1) lib/thin/connection.rb:53:in `process'
thin (1.4.1) lib/thin/connection.rb:38:in `receive_data'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run_machine'
eventmachine (1.0.0) lib/eventmachine.rb:187:in `run'
thin (1.4.1) lib/thin/backends/base.rb:63:in `start'
thin (1.4.1) lib/thin/server.rb:159:in `start'
rack (1.4.1) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.1) lib/rack/server.rb:265:in `start'
railties (3.2.8) lib/rails/commands/server.rb:70:in `start'
railties (3.2.8) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.8) lib/rails/commands.rb:50:in `tap'
railties (3.2.8) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
like image 327
zazenergy Avatar asked Nov 03 '22 16:11

zazenergy


1 Answers

You need to add a username column to your users table.

like image 82
jamesharker Avatar answered Nov 15 '22 11:11

jamesharker