Ruby Rails 用户的电子邮件被覆盖

Ruby Rails User's Email Being overwritten

从头开始尝试身份验证,但出于某种原因,我的用户电子邮件地址未保存到数据库中。当我启动控制台并拉取一个随机用户时,他们的电子邮件始终为零,但为电子邮件的存在设置了验证,并且我收到了用户创建成功的消息。这以前是有效的,但不确定是什么破坏了它。我知道这是一件非常简单的事情,但在过去的几个小时里它一直困扰着我,我希望第二双眼睛能告诉我我做错了什么。 GitHub link 下面 [https://github.com/smarandache1990/auth][1]

        <h1>Sign Up</h1>

        <%= form_for @user do |f| %>
          <% if @user.errors.any? %>
            <div class="error_messages">
              <h2>Form is invalid</h2>
              <ul>
                <% for message in @user.errors.full_messages %>
                  <li><%= message %></li>
                <% end %>
              </ul>
            </div>
          <% end %>
          <p>
            <%= f.label :email %><br />
            <%= f.text_field :email %>
            <% @user.errors.full_messages_for(:email).each do |message| %>
              <div><%= message %></div>
            <% end %>
          </p>
          <p>
            <%= f.label :password %><br />
            <%= f.password_field :password %>
            <% @user.errors.full_messages_for(:password).each do |message| %>
              <div><%= message %></div>
            <% end %>
          </p>
          <p>
            <%= f.label :password_confirmation %><br />
            <%= f.password_field :password_confirmation %>
            <% @user.errors.full_messages_for(:password_confirmation).each do |message| %>
              <div><%= message %></div>
            <% end %>
          </p>
          <p class="button"><%= f.submit %></p>
        <% end %>
        <h1>Sign In</h1>

        <%= form_tag sessions_path do %>

            <p>
                <%= label_tag :email %><br />
                <%= text_field_tag :email, params[:email] %>
            </p>
            <p>
                <%= label_tag :password %><br />
                <%= password_field_tag :password %>
            </p>
            <p class="button"><%= submit_tag "Log in" %></p>

        <% end %>
            class User < ApplicationRecord
                attr_accessor :email, :password, :password_confirmation

                before_save :encrypt_password
                
                validates :password, confirmation: true, on: :create 
                validates :password_confirmation, presence: true
                
                #validates_presence_of :password, :on => :create
                #validates_confirmation_of :password
                validates_presence_of :email
                validates_uniqueness_of :email
                
                def self.authenticate(email, password)
                user = find_by_email(email)
                if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
                    user
                else
                    nil
                end
                end
                
                def encrypt_password
                if password.present?
                    self.password_salt = BCrypt::Engine.generate_salt
                    self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
                end
                end
            end

    class UsersController < ApplicationController
      def new
        @user = User.new
      end
      
      def create
        @user = User.new(user_params)
        if @user.save
          redirect_to log_in_path, :notice => "Signed up!"
        else
          render :new, status: :unprocessable_entity
        end
      end

      private
      def user_params
        params.require(:user).permit(:email, :password, :password_confirmation)
      end
    end
        class SessionsController < ApplicationController
          def new
          end
          
          def create
            @user = User.authenticate(params[:email], params[:password])
            if @user
              session[:user_id] = @user.id
              redirect_to root_url, :notice => "Logged in!"
            else
              flash.now[:notice] = "Wrong username or password"
              render :new, status: :unprocessable_entity
            end
          end
          
          def destroy
            session[:user_id] = nil
            redirect_to root_url, :notice => "Logged out!"
          end
          
        end

attr_acessor (here) 中删除 email

attr_accessor 为此处列出的所有属性创建 getter 和 setter。所以,基本上,您要覆盖 email 属性的这些方法。

关于验证,使用这个:

validates :email, presence: true, uniqueness: true