首页 > 解决方案 > 用户在 Rails 上注册 ruby​​ 时存在错误acts_as_paranoid

问题描述

我在注册时遇到错误。控制台显示它的错误“用户存在”但是我认为它的问题是 act_as_paranoid 与设计 gem 冲突。你能帮忙吗?

宝石 - 设备和行为偏执狂

在我添加acts_as_paranoid gem之前它工作正常

控制台响应:

Started POST "/users" for 127.0.0.1 at 2018-09-18 20:25:17 +1000
Processing by Users::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"WBPVa4QVwzij/j1H+6uOMNURddc2CQX/YJJ+pIKXi3mRwa4aIgOcYbwQKsPGO5sjFYUlC89lH1mn7SpmkYZ1qw==", "user"=>{"first_name"=>"Ben", "last_name"=>"Strachan", "email"=>"ben@ownerhealth.com.au", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}
   (0.2ms)  BEGIN
  User Exists (0.8ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 AND "users"."deleted_at" IS NULL LIMIT $2  [["email", "ben@ownerhealth.com.au"], ["LIMIT", 1]]
   (0.2ms)  ROLLBACK
  Rendering devise/registrations/new.html.erb within layouts/auth
  Rendered devise/shared/_links.html.erb (0.6ms)
  Rendered devise/registrations/new.html.erb within layouts/auth (7.5ms)
   (0.3ms)  BEGIN
  User Exists (0.5ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 AND "users"."deleted_at" IS NULL LIMIT $2  [["email", "ben@ownerhealth.com.au"], ["LIMIT", 1]]
   (0.2ms)  ROLLBACK
Completed 200 OK in 198ms (Views: 32.5ms | ActiveRecord: 2.2ms)

用户型号:

    # == Schema Information
#
# Table name: users
#
#  id                     :bigint(8)        not null, primary key
#  email                  :string           default(""), not null
#  encrypted_password     :string           default(""), not null
#  reset_password_token   :string
#  reset_password_sent_at :datetime
#  remember_created_at    :datetime
#  sign_in_count          :integer          default(0), not null
#  current_sign_in_at     :datetime
#  last_sign_in_at        :datetime
#  current_sign_in_ip     :string
#  last_sign_in_ip        :string
#  created_at             :datetime         not null
#  updated_at             :datetime         not null
#  first_name             :string
#  last_name              :string
#  role                   :string
#  invitation_token       :string
#  invitation_created_at  :datetime
#  invitation_sent_at     :datetime
#  invitation_accepted_at :datetime
#  invitation_limit       :integer
#  invited_by_type        :string
#  invited_by_id          :integer
#  invitations_count      :integer          default(0)
#  avatar_file_name       :string
#  avatar_content_type    :string
#  avatar_file_size       :integer
#  avatar_updated_at      :datetime
#  business_id            :integer
#  author_id              :integer
#  deleted_at             :datetime
#

class User < ApplicationRecord
  acts_as_paranoid

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  ROLES = [
    ROLE_ADMIN = "Admin",
    ROLE_REGULAR = "Regular"
]

devise :invitable, :database_authenticatable, :registerable,
       :recoverable, :rememberable, :trackable, :validatable


validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
validates :role, inclusion: ROLES, presence: true

has_attached_file :avatar, styles: { medium: "450x450>" },
                          default_url: -> (attachment) {
                            ActionController::Base.helpers.asset_path(
                              'default-avatar.png'
                            )
                          }
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\z/

before_validation :set_role
belongs_to :business, optional: true


def full_name
  [first_name, last_name].join(" ")
end

def admin?
  self.role == ROLE_ADMIN
end

private
def set_role
  self.role = ROLE_REGULAR if self.role.blank?
end
end

注册控制器:

    class Users::RegistrationsController < Devise::RegistrationsController
  layout 'auth'
  # before_action :configure_sign_up_params, only: [:create]
  # before_action :configure_account_update_params, only: [:update]

  # GET /resource/sign_up
  # def new
  #   super
  # end

  # POST /resource
  def create
    super
    if resource.save
      business = Business.create first_name: resource.first_name,
                                 last_name: resource.first_name,
                                 email: resource.email
      resource.update business_id: business.id
    end
  end

架构:

      create_table "users", force: :cascade do |t|
    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"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "first_name"
    t.string "last_name"
    t.string "role"
    t.string "invitation_token"
    t.datetime "invitation_created_at"
    t.datetime "invitation_sent_at"
    t.datetime "invitation_accepted_at"
    t.integer "invitation_limit"
    t.string "invited_by_type"
    t.integer "invited_by_id"
    t.integer "invitations_count", default: 0
    t.string "avatar_file_name"
    t.string "avatar_content_type"
    t.integer "avatar_file_size"
    t.datetime "avatar_updated_at"
    t.integer "business_id"
    t.integer "author_id"
    t.datetime "deleted_at"
    t.index ["deleted_at"], name: "index_users_on_deleted_at"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
    t.index ["invitations_count"], name: "index_users_on_invitations_count"
    t.index ["invited_by_id"], name: "index_users_on_invited_by_id"
    t.index ["invited_by_type", "invited_by_id"], name: "index_users_on_invited_by_type_and_invited_by_id"
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

标签: ruby-on-rails

解决方案


我犯了一个错误!用户 author_id 关系错误。这已经解决了。

属于:作者,类名:'用户',外键:'作者ID',可选:真


推荐阅读