弃用警告(哈特尔教程)
问题描述:
我通过迈克尔·哈特尔的Rails的教程工作,当我运行我的测试套件,我看到了这样的错误:弃用警告(哈特尔教程)
DEPRECATION WARNING: You attempted to assign a value which is not explicitly
`true` or `false` to a boolean column. Currently this value casts to `false`.
This will change to match Ruby's semantics, and will cast to `true` in Rails
5. If you would like to maintain the current behavior, you should explicitly
handle the values you would like cast to `false`. (called from remember at
.../RoR_Tutorial/sample_app/app/models/user.rb:28)
DEPRECATION WARNING: You attempted to assign a value which is not explicitly
`true` or `false` to a boolean column. Currently this value casts to `false`.
This will change to match Ruby's semantics, and will cast to `true` in Rails
5. If you would like to maintain the current behavior, you should explicitly
handle the values you would like cast to `false`. (called from update at
...RoR_Tutorial/sample_app/app/controllers/users_controller.rb:40)
这似乎是愤怒的电话到update_attribute
像这样:
def remember
self.remember_token = User.new_token
update_attribute(:remember_digest, User.digest(remember_token))
end
或
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = 'Profile Updated'
redirect_to @user
else
render 'edit'
end
end
...任何人都可以澄清这个警告试图告诉我什么?
答
所以事实证明,我的YAML文件中有一个错误,用于创建我的测试用户。
lana:
name: Lana Kane
email: [email protected]
password_digest: <%= User.digest('password') %>
activated: true,
activated_at: <%= Time.zone.now %>
...注意在activated:
行末尾有逗号逗号。这意味着我实际上没有将值设置为布尔值true
(猜测它将该视图视为5个字符的字符串?)。
通过校正YAML到
lana:
name: Lana Kane
email: [email protected]
password_digest: <%= User.digest('password') %>
activated: true
activated_at: <%= Time.zone.now %>
...(没有逗号在activated
行的结尾)错误消失。
答
看来你的数据库有一些boolean
类型的列,这意味着它们的值被限制为true
或false
。据废弃警告,你在你的User
模型update_attributes
和update_attribute
和users_controller
电话设置这些属性的值到的东西比true
或false
(尽管它被强制转换为那两个选项之一反正)不同。
只要你下面的教程,似乎没有什么可担心的:你已经警告说,不同的价值观的改造,以true
或false
的algorighm会在Rails
即将发布改变。
虽然Hartl的教程可能有点过时,但看到这些类型的铸件仍然有点奇怪。您可能需要仔细检查您的schema.rb
和本书中列出的迁移文件,以确保您的设置绝对正确。