簡體   English   中英

Rails不保存模型字段

[英]Rails not saving model field

我想知道為什么我的rails不保存我的encryption_password字段。

這是我的UserController

class UserController < ApplicationController
  require 'bcrypt'
  before_filter :save_login_state, :only => [:new, :create]
  def new
    new_user = User.new(user_params)
    new_user.numRatings = 0    
    if new_user.save
      flash[:notice] = "You signed up successfully"
      flash[:color]= "valid"
    else
      flash[:notice] = "Form is invalid"
      flash[:color]= "invalid"
    end
    redirect_to(:controller => 'sessions', :action => 'login')
  end

  def create
  end

  def update
  end

  def view
  end
  private
    def user_params
      params.require(:user).permit(:name, :email, :password, :password_confirmation)
    end
end

這是我的用戶模型

class User < ActiveRecord::Base
    require 'BCrypt'
    attr_accessor :password, :encrypted_password
    EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
    validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
    validates :name, :presence => true
    validates :password, :confirmation => true, :presence => true, :on => :create

    before_save :encrypt_password
    after_save :clear_password

    def encrypt_password
      if password.present?
        self.salt = BCrypt::Engine.generate_salt
        self.encrypted_password = BCrypt::Engine.hash_secret(password, salt)
      end
    end

    def clear_password
      self.password = nil
    end

    def self.authenticate(username_or_email="", login_password="")
      if  EMAIL_REGEX.match(username_or_email)    
        user = User.find_by_email(username_or_email)
      end
      if user && user.match_password(login_password)
        return user
      else
        return false
      end
    end   

    def match_password(login_password="")
      encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
    end

end

另外,我用這個功能來保存它

  def login_attempt
    authorized_user = User.authenticate(params["user"]["email"],params["user"]["password"])
    if authorized_user
        session[:user_id] = authorized_user.id
        flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}"
        redirect_to(:action => 'home')
    else
        flash[:notice] = "Invalid Username or Password"
        flash[:color]= "invalid"
        render "login"  
    end
  end

我懷疑我創建了第一次遷移的一件事

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :email
      t.string :password
      t.string :name
      t.float :rating
      t.integer :numRatings

      t.timestamps
    end
  end
end

但是,我將:password字段更改為:encrypted_pa​​ssword,並將其反映在表中。 我已經堅持了兩個小時。 我想知道是否還有任何東西。 謝謝。

日志顯示正在注入數據減去加密密碼

插入“用戶”(“ created_at”,“電子郵件”,“名稱”,“ numRatings”,“鹽”,“ updated_at”)值(?,?,?,?,?,?)[[“ created_at”, “ 2014-08-05 06:22:41.335373”],[“電子郵件”,“ w@w.com”],[“名稱”,“ w”],[“ numRatings”,0],[“鹽” ,“ $ 2a $ 10 $ pagmnNzT4FWEBmaPmiLX1u”],[“ updated_at”,“ 2014-08-05 06:22:41.335373”]]

您的attr_accessor :encrypted_password突出顯示-這將覆蓋Rails生成的屬性getter / setter,該屬性將在模型實例中簡單設置一個名為@encrypted_password的實例變量。

您提到您將Migration :password field更改為:encrypted_password field ,但是您確定要重新初始化模型嗎? 即正確遷移了更改? 最后,如果您確實更改了遷移,則無需使用attr_accessorencrypted_password 同時擁有它們可能會導致陰影並成為問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM