0
我試圖爲我的Ruby on Rails
應用程序創建一個User
模型,該應用程序的password
字段未保存在數據庫中,但由於某種原因未正確更新。不支持數據庫未更新的屬性
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
我使用了一個非常簡單的表單更新email
和password
。
= 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'
在我的控制器我試圖使用基本的更新機制,但我願意添加更多的代碼,如果我絕對必須。
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
當這個更新處理它引發與所述消息的TypeError
:can't convert nil into String
。
我試過幾種不同的方法來解決這個問題,包括使用params[:user][:password]
手動設置password
,但它不起作用。任何人都可以發現我錯過的錯誤嗎?
您是否試過attr_accessible? – 2012-02-28 16:53:36
什麼是attr_accessible? 'attr'的插件? – 2012-02-28 16:55:18