2012-02-28 6 views
0

데이터베이스에 저장되지 않은 password 필드가있는 Ruby on Rails 응용 프로그램의 User 모델을 만들려고하지만 어떤 이유로 올바르게 업데이트되지 않습니다. 데이터베이스에서 업데이트되지 않는 특성

class User < ActiveRecord::Base 
    attr :password 
    validates_presence_of :email, :password_digest 

    def password! 
    password_digest = Digest::SHA1.hexdigest(password) 
    end 

    def password?(password) 
    password_digest == Digest::SHA1.hexdigest(password) 
    end 
end 

은 내가 emailpassword를 업데이트 할 아주 기본적인 양식을 사용하고 있습니다. 내가 절대적으로 필요하다면 더 많은 코드를 추가 기꺼이하지만 내 컨트롤러에서

= form_for @user do |f| 
    -if @user.errors.any? 
    #error_explanation 
     %h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:" 
     %ul 
     - @user.errors.full_messages.each do |msg| 
      %li= msg 

    .field 
    = f.label :email 
    = f.email_field :email, :required => true 
    .field 
    = f.label :password 
    = f.password_field :password, :required => true 
    .actions 
    = f.submit 'Save' 

나는, 기본 업데이트 메커니즘을 사용하는 것을 시도하고있다. 이 업데이트는 메시지와 함께 TypeError을 발생 처리

class UsersController < ApplicationController 
    def create 
    @user = User.new(params[:user]) 

    @user.password! 

    respond_to do |format| 
     if @user.save 
     format.html { redirect_to @user, notice: 'User was successfully created.' } 
     format.json { render json: @user, status: :created, location: @user } 
     else 
     format.html { render action: "new" } 
     format.json { render json: @user.errors, status: :unprocessable_entity } 
     end 
    end 
    end 
end 

: can't convert nil into String.

params[:user][:password]을 사용하여 수동으로 password을 설정하는 것을 포함하여 여러 가지 방법으로 문제를 해결하려고했지만 작동하지 않습니다. 아무도 내가 놓친 버그를 발견 할 수 있습니까?

+0

attr_accessible로 시도 했습니까? –

+0

'attr_accessible'이란 무엇입니까? 'attr'을위한 드롭 인? –

답변

2

비밀번호를 잊어 버렸습니다. 메소드에서 password_digest 인스턴스 변수에 액세스하도록 지정해야합니다. 변경 대상 :

def password! 
    self.password_digest = Digest::SHA1.hexdigest(password) 
end