RegistrationsController中的NameError#使用devise登录创建

时间:2014-07-25 18:48:58

标签: devise

我有一个项目,我在Facebook,Twitter和LinkedIn上使用omniauth。它也使用Devise登录gem。当用户进入注册页面创建登录并输入电子邮件并选择密码时,会显示错误页面NameError in RegistrationsController#create

这出现在终端:

Started POST "/users" for 127.0.0.1 at 2014-07-25 14:42:30 -0400
Processing by RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"MMYGtL6b6AyEuWIWwpxMg7qn8TWGXinMZTQ2K0GDsyU=", "user"=>{"email"=>"Phil9767@yahoo.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
   (0.1ms)  begin transaction
  User Exists (0.5ms)  SELECT  1 AS one FROM "users"  WHERE "users"."email" = 'phil9767@yahoo.com' LIMIT 1
   (0.1ms)  rollback transaction
Completed 500 Internal Server Error in 91ms

NameError (undefined local variable or method `confirmed_at' for #<User:0x00000106162ef0>):
  activemodel (4.1.1) lib/active_model/attribute_methods.rb:435:in `method_missing'
  activerecord (4.1.1) lib/active_record/attribute_methods.rb:206:in `method_missing'
  devise (3.2.4) lib/devise/models/confirmable.rb:88:in `confirmed?'
  devise (3.2.4) lib/devise/models/confirmable.rb:163:in `confirmation_required?'
  activesupport (4.1.1) lib/active_support/callbacks.rb:424:in `block in make_lambda'
  activesupport (4.1.1) lib/active_support/callbacks.rb:142:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:142:in `block (2 levels) in halting_and_conditional'
  activesupport (4.1.1) lib/active_support/callbacks.rb:142:in `each'
  activesupport (4.1.1) lib/active_support/callbacks.rb:142:in `all?'
  activesupport (4.1.1) lib/active_support/callbacks.rb:142:in `block in halting_and_conditional'
  activesupport (4.1.1) lib/active_support/callbacks.rb:215:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:215:in `block in halting_and_conditional'
  activesupport (4.1.1) lib/active_support/callbacks.rb:215:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:215:in `block in halting_and_conditional'
  activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
  activerecord (4.1.1) lib/active_record/callbacks.rb:306:in `create_record'
  activerecord (4.1.1) lib/active_record/timestamp.rb:57:in `create_record'
  activerecord (4.1.1) lib/active_record/persistence.rb:482:in `create_or_update'
  activerecord (4.1.1) lib/active_record/callbacks.rb:302:in `block in create_or_update'
  activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
  activerecord (4.1.1) lib/active_record/callbacks.rb:302:in `create_or_update'
  activerecord (4.1.1) lib/active_record/persistence.rb:103:in `save'
  activerecord (4.1.1) lib/active_record/validations.rb:51:in `save'
  activerecord (4.1.1) lib/active_record/attribute_methods/dirty.rb:21:in `save'
  activerecord (4.1.1) lib/active_record/transactions.rb:268:in `block (2 levels) in save'
  activerecord (4.1.1) lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `block in transaction'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
  activerecord (4.1.1) lib/active_record/transactions.rb:208:in `transaction'
  activerecord (4.1.1) lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
  activerecord (4.1.1) lib/active_record/transactions.rb:268:in `block in save'
  activerecord (4.1.1) lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
  activerecord (4.1.1) lib/active_record/transactions.rb:267:in `save'
  devise (3.2.4) app/controllers/devise/registrations_controller.rb:15:in `create'
  actionpack (4.1.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (4.1.1) lib/abstract_controller/base.rb:189:in `process_action'
  actionpack (4.1.1) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (4.1.1) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
  activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:113:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:229:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:166:in `block in halting'
  activesupport (4.1.1) lib/active_support/callbacks.rb:149:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.1) lib/active_support/callbacks.rb:149:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:149:in `block in halting_and_conditional'
  activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:86:in `run_callbacks'
  actionpack (4.1.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.1.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
  activesupport (4.1.1) lib/active_support/notifications.rb:159:in `block in instrument'
  activesupport (4.1.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.1.1) lib/active_support/notifications.rb:159:in `instrument'
  actionpack (4.1.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.1.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.1.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.1.1) lib/abstract_controller/base.rb:136:in `process'
  actionview (4.1.1) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.1.1) lib/action_controller/metal.rb:195:in `dispatch'
  actionpack (4.1.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.1.1) lib/action_controller/metal.rb:231:in `block in action'
  actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `call'
  actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:80:in `dispatch'
  actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:48:in `call'
  actionpack (4.1.1) lib/action_dispatch/routing/mapper.rb:45:in `call'
  actionpack (4.1.1) lib/action_dispatch/journey/router.rb:71:in `block in call'
  actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `each'
  actionpack (4.1.1) lib/action_dispatch/journey/router.rb:59:in `call'
  actionpack (4.1.1) lib/action_dispatch/routing/route_set.rb:676:in `call'
  omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.1) lib/omniauth/strategy.rb:186:in `call!'
  omniauth (1.2.1) lib/omniauth/strategy.rb:164:in `call'
  omniauth (1.2.1) 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.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.1.1) 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.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.1.1) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:621:in `call'
  activerecord (4.1.1) lib/active_record/migration.rb:380:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.1.1) lib/active_support/callbacks.rb:82:in `run_callbacks'
  actionpack (4.1.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
  activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
  railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
  actionpack (4.1.1) 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.1) 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.1) lib/action_dispatch/middleware/static.rb:64:in `call'
  rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
  railties (4.1.1) lib/rails/engine.rb:514:in `call'
  railties (4.1.1) 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'
  /Users/TheMac/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
  /Users/TheMac/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
  /Users/TheMac/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'


  Rendered /Users/TheMac/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.8ms)
  Rendered /Users/TheMac/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendered /Users/TheMac/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (10.6ms)
  Rendered /Users/TheMac/.rvm/gems/ruby-2.1.1/gems/actionpack-4.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (28.6ms)

我已经在stackoverflow上做了很多次搜索,看起来似乎没有关联中的身份验证是多元化的,也似乎与跳过设计中的身份验证无关。我创建了一个注册控制器,但它似乎没有帮助。关于问题可能是什么想法?

这是我的用户模型

    class User < ActiveRecord::Base
  has_many :authentications

  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

这是我的schema.rb

 ActiveRecord::Schema.define(version: 20140724042203) do

  create_table "identities", force: true do |t|
    t.integer  "user_id"
    t.string   "provider"
    t.string   "uid"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "identities", ["user_id"], name: "index_identities_on_user_id"

  create_table "users", force: true do |t|
    t.string   "provider"
    t.string   "uid"
    t.string   "name"
    t.string   "oauth_token"
    t.datetime "oauth_expires_at"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    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

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

1 个答案:

答案 0 :(得分:0)

您的模型用户缺少“confirmed_at”属性。

您可以使用新迁移创建它:

rails g migration add_unconfirmed_to_devise

并输入:

    class AddUnconfirmedToDevise < ActiveRecord::Migration
  def change
    change_table(:users) do |t|
    #t.string   :confirmation_token
    #t.datetime :confirmed_at
    #t.datetime :confirmation_sent_at
    #t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
    #t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
    #t.string   :unlock_token # Only if unlock strategy is :email or :both
    #t.datetime :locked_at
    end
  end
end

只需取消注释您需要的每个属性。