在Rails中创建没有密码的用户

时间:2017-08-13 18:52:43

标签: ruby-on-rails authentication

我有三个使用枚举定义的用户角色和一个带有STI的单个用户表。我的三个用户角色是员工,临床医生和列出的。目标是工作人员将上传名称和电子邮件列表,这些列表将创建列出的角色用户。一旦这些列出的用户创建自己的密码,他们的角色将变为临床医生。

我正在调整我的模型和控制器以允许创建这些列出的用户但遇到困难,因为我发现的大部分答案都涉及到Devise实现。我正在使用一个主要使用Michael Hartl的Rails Tutorial构建的身份验证系统。你有什么关于如何让它发挥作用的提示吗?目前,当我尝试以列出的用户身份注册时,出现以下错误:

列出的未知属性'密码'。

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."

任何帮助都将非常感谢。谢谢!

以下是我的用户模型:

class User < ApplicationRecord
  self.inheritance_column = :role
  enum role: { Staff: 0, Clinician: 1, Listed: 2 }


  attr_accessor :remember_token, :activation_token, :reset_token
  before_save   :downcase_email
  before_create :create_activation_digest
  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }
  validates :role, presence: true


  # Returns the hash digest of the given string.
  def User.digest(string)
    cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST :
                                                  BCrypt::Engine.cost
    BCrypt::Password.create(string, cost: cost)
  end

  # Returns a random token.
  def User.new_token
    SecureRandom.urlsafe_base64
  end

  # Remembers a user in the database for use in persistent sessions.
  def remember
    self.remember_token = User.new_token
    update_attribute(:remember_digest, User.digest(remember_token))
  end

  # Returns true if the given token matches the digest.
  def authenticated?(remember_token)
        return false if remember_digest.nil?
    BCrypt::Password.new(remember_digest).is_password?(remember_token)
  end


  # Forgets a user.
  def forget
    update_attribute(:remember_digest, nil)
  end

    # Returns true if the given token matches the digest.
  def authenticated?(attribute, token)
    digest = send("#{attribute}_digest")
    return false if digest.nil?
    BCrypt::Password.new(digest).is_password?(token)
  end

  # Activates an account.
  def activate
    update_attribute(:activated,    true)
    update_attribute(:activated_at, Time.zone.now)
  end

  # Sends activation email.
  def send_activation_email
    UserMailer.account_activation(self).deliver_now
  end

 # Sets the password reset attributes.
  def create_reset_digest
    self.reset_token = User.new_token
    update_attribute(:reset_digest,  User.digest(reset_token))
    update_attribute(:reset_sent_at, Time.zone.now)
  end

  # Sends password reset email.
  def send_password_reset_email
    UserMailer.password_reset(self).deliver_now
  end

   # Returns true if a password reset has expired.
  def password_reset_expired?
    reset_sent_at < 2.hours.ago
  end

 def feed
    ReferralRequest.where("user_id = ?", id)
  end


 private

    # Converts email to all lower-case.
    def downcase_email
    self.email = email.downcase
    end

    # Creates and assigns the activation token and digest.
    def create_activation_digest
      self.activation_token  = User.new_token
      self.activation_digest = User.digest(activation_token)
    end


end


class Staff < User
  validates :university_id, presence: true
  belongs_to :university
  has_many :referral_requests
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
end

class Clinician < User
  has_many :lists
  has_many :universities, through: :lists
  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
end

class Listed < User
  has_many :lists
  has_many :universities, through: :lists
end

这是我的用户控制器:

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:show, :index, :edit, :update, :destroy]
  before_action :correct_user,   only: [:edit, :update]

  def index
    @users = User.paginate(page: params[:page])
  end

   def show

    @user = User.find(params[:id])
    @referral_requests = @user.referral_requests.paginate(page: params[:page])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      @user.send_activation_email
      flash[:info] = "Please check your email to activate your account."
      redirect_to root_url
    else
        render 'new'
    end
  end

  def edit
  end

  def update
    if @user.update_attributes(user_params)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

def destroy
  User.find(params[:id]).destroy
  flash[:success] = "User deleted"
  redirect_to users_url
end

    private

        def user_params
            params.require(:user).permit(:name, :email, :role, :university_id, :password, :password_confirmation)

  end

    # Before filters


    # Confirms the correct user.
    def correct_user
      @user = User.find(params[:id])
      redirect_to(root_url) unless current_user?(@user)
    end

end

1 个答案:

答案 0 :(得分:0)

我不确定STI是否是管理角色的最佳方法,无论如何,如果您的User类具有属性password并且您正确地进行了继承,Listed应该也有这个属性。

您的Listed模型应如下所示:

Class Listed < User
end

与从User

继承的所有模型相同