Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 存储在不同型号上的Rails身份验证用户和密码_Ruby On Rails_Ruby_Authentication - Fatal编程技术网

Ruby on rails 存储在不同型号上的Rails身份验证用户和密码

Ruby on rails 存储在不同型号上的Rails身份验证用户和密码,ruby-on-rails,ruby,authentication,Ruby On Rails,Ruby,Authentication,我在做一个项目,我需要有不同类型的用户,有些是管理员,有些只是用户,我有一个适用于所有用户的模型,我有一个“登录”模型,我会在其中存储加密密码。问题是,我创建了用户,它保存在数据库中,但是应该存储密码的模型是空的。。。 我试图对用户和登录控制器以及两种模型进行关联 问我你需要看的代码,我会把它贴在这里 这是我的user.rb代码 class User < ActiveRecord::Base has_and_belongs_to_many :requisitions

我在做一个项目,我需要有不同类型的用户,有些是管理员,有些只是用户,我有一个适用于所有用户的模型,我有一个“登录”模型,我会在其中存储加密密码。问题是,我创建了用户,它保存在数据库中,但是应该存储密码的模型是空的。。。 我试图对用户和登录控制器以及两种模型进行关联

问我你需要看的代码,我会把它贴在这里

这是我的user.rb代码

    class User < ActiveRecord::Base
      has_and_belongs_to_many :requisitions
      has_many :historics
      has_one :login
      belongs_to :rank
      belongs_to :sub_unit
      belongs_to :user_type
      #attr_accessor :password, :password_confirmation, :salt, :encrypted_password

      validates :nim, :posto_id, :apelido, :nome, :telefone, :sub_un_id, :tipo_util_id, presence: true
      validates :email, format: { with: /@/ }
      validates :nim, uniqueness: true
      validates :password, :confirmation => true
      validates_length_of :password, :in => 6..20, :on => :create

      # Encrypting calls for new user's password
      before_save :encrypt_password
      after_save :clear_password

      # Authentication process for user's login
      def match_password(login_password = '')
      encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
      end

      def self.authenticate(nim_as_login = '', login_password = '')
        user = User.find_by_nim(nim_as_login)
        if user && user.match_password(login_password)
          return user
        else
          return false
        end
      end

      # Encrypting process for new user's password
      private
      def encrypt_password
        if password.present?
          self.salt = BCrypt::Engine.generate_salt
          self.encrypted_password = BCrypt::Engine.hash_secret(password, salt)
        end
      end

      private
      def clear_password
        self.password = nil
      end

    end
class用户true
验证以下内容的长度:password,:in=>6..20,:on=>:create
#加密对新用户密码的调用
保存前:加密密码
保存后:清除密码
#用户登录的身份验证过程
def匹配密码(登录密码=“”)
加密密码==BCrypt::Engine.hash\u secret(登录密码,salt)
终止
def self.authenticate(nim_as_login='',login_password='')
user=user.find\u by\u nim(nim\u作为\u登录)
如果用户&&user.match\u密码(登录密码)
返回用户
其他的
返回错误
终止
终止
#新用户密码的加密过程
私有的
def加密密码
如果密码存在?
self.salt=BCrypt::Engine.generate\u salt
self.encrypted_password=BCrypt::Engine.hash_secret(密码,salt)
终止
终止
私有的
def清除密码
self.password=nil
终止
终止
这是我的login.rb代码

    class Login < ActiveRecord::Base
      belongs_to :user

      attr_accessor :password, :password_confirmation, :salt, :encrypted_password

      #validating fields
      validates :user_id, :password, presence: true
      validates :password, confirmation: true
      validates :password, length: { in: 6..30 }

      # Encrypting calls for new user's password
      before_save :encrypt_password
      after_save :clear_password

      # Authentication process for user's login
      def match_password(login_password = '')
        encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
      end

      def self.authenticate(nim_as_login = '', login_password = '')
        user = User.find_by_nim(nim_as_login)
        login = Login.find_by_user_id(user.id)
        if login && login.match_password(login_password)
          return login
        else
          return false
        end
      end

      # Encrypting process for new user's password
      private
      def encrypt_password
        if password.present?
          self.salt = BCrypt::Engine.generate_salt
          self.encrypted_password = BCrypt::Engine.hash_secret(password, salt)
        end
      end

      private
      def clear_password
        self.password = nil
      end


    end
类登录
这是我的用户控制器代码

    class UsersController < ApplicationController

      before_filter :save_login_state, only: [:new, :create]

      def new
        @user = User.new
        @ranks = Rank.all
        @types = UserType.all
        @unit = SubUnit.all
        @logins = Login.all
        @page_title = "LoginUser | Novo"
      end

      def create
        @user = User.new(user_params, login_params)

        #LoginsController(:password, :password_confirmation)

        @ranks = Rank.all
        @types = UserType.all
        @unit = SubUnit.all
        @logins = Login.all
        if @user.save
          flash[:notice] = "Bem vindo #{@user.apelido}, sua conta foi criada com sucesso!"
          redirect_to sessions_path
        else
          @user.errors.full_messages.each do |e|
            if e == "Nim has already been taken"
              flash.now[:error] = "Este Utilizador já está registado!"
            else
              flash.now[:error] = "Corrija os campos do formulário!"
            end
          end
          render 'new'
        end
      end
      def show
        @user = User.find(params[:id])
        @ranks = Rank.all
        @types = UserType.all
        @unit = SubUnit.all
        @logins = Login.all
      end

      private

      def user_params
        params.require(:user).permit(:id, :nim, :posto_id, :apelido, :nome, :telefone, :telemovel, :email, :sub_un_id, :tipo_util_id)
      end

      def login_params
        params.require(Login).permit(:password, :password_confirmation, :user_id )
      end

    end
class UsersController
这是我的登录控制器

    class LoginsController < ApplicationController

      def create
       @login = Login.all

      end

    end
    class SessionsController < ApplicationController
      before_filter :authenticate_user, only: [:home, :profile, :setting]
      before_filter :save_login_state, only: [:login, :login_attempt]

      def login
        @page_title = "LoginUser | Entrar"
      end

      def login_attempt
        authorized_user = User.authenticate(params[:nim_as_login], params[:login_password])
        if authorized_user
          session[:user_id] = authorized_user.id
          flash[:notice] = "Benvindo de volta #{authorized_user.apelido}"
          redirect_to user_path
        else
          flash.now[:error] = "Email ou palavra passe inválida!"
          render 'sessions/login'
        end
      end

      def home
      end

      def profile
      end

      def setting
      end

      def logout
        session[:user_id] = nil
        redirect_to sessions_path
      end
    end
class LoginsController
这是我的会话控制器

    class LoginsController < ApplicationController

      def create
       @login = Login.all

      end

    end
    class SessionsController < ApplicationController
      before_filter :authenticate_user, only: [:home, :profile, :setting]
      before_filter :save_login_state, only: [:login, :login_attempt]

      def login
        @page_title = "LoginUser | Entrar"
      end

      def login_attempt
        authorized_user = User.authenticate(params[:nim_as_login], params[:login_password])
        if authorized_user
          session[:user_id] = authorized_user.id
          flash[:notice] = "Benvindo de volta #{authorized_user.apelido}"
          redirect_to user_path
        else
          flash.now[:error] = "Email ou palavra passe inválida!"
          render 'sessions/login'
        end
      end

      def home
      end

      def profile
      end

      def setting
      end

      def logout
        session[:user_id] = nil
        redirect_to sessions_path
      end
    end
class sessioncontroller