ruby - mysql - Not saving password field

时间:2018-12-19 11:35:29

标签: ruby-on-rails ruby

I'm having a problem that I have never found. I have a form to create a new user, in it, I have the field to save my password with has_secure_password

<div class="row">
  <div class="col-md-4">
    <div class="form-group">
      <%= form.label "Contraseña" %>
      <%= form.password_field :password_digest, class:"form-control", id: :user_password_digest %>
    </div>
  </div>
</div>

The question is that all the fields are been saved correctly, but password field is always NULL.

My model:

class User < ApplicationRecord
  has_one :rol

  #attr_accessible :email, :password
  attr_accessor :password_digest

  validates_confirmation_of :password_digest
  #validates_presence_of :password, :on => :createrail
  validates :password_digest, presence: true, length: {minimum: 5}, allow_nil: false
  validates_presence_of :email, :on => :create
  validates_uniqueness_of :email
  has_secure_password

  def self.authenticate(email, password_digest)
    user = find_by_email(email)
    p user
    if user && user.password_digest
      p "Me he logueado"
      user
    else
      p "No hace hash"
      nil
    end
  end

  def logged_in
    !current_user.nil?
  end

My controller:

def create
@user = User.new(user_params)

p "Parametros"
p user_params
p @user
respond_to do |format|
  if @user.valid?

    @user.active = 1
    if @user.save
      p "El usuario es"
      p @user

      session[:usuario_id] = @user.id
      p "Test"
      p @user.password_digest

      p "EndTest"
      format.html { redirect_to @user, notice: 'Usuario creado correctamente.' }
      format.json { render :show, status: :created, location: @user }
    else
      format.html { render :new }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end  
end

In test, I can see that the password_digest field has content, but later, it is not saving. Any help?

1 个答案:

答案 0 :(得分:0)

我不知道,但是我认为如果您使用的是Rails 4或更高版本,则不应使用以下代码,因为有一个参数很强的概念。一次查看指南

attr_accessor :password_digest