2016-06-12 119 views
0

我通過邁克爾·哈特爾的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 

...任何人都可以澄清這個警告試圖告訴我什麼?

回答

0

所以事實證明,我的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行的結尾)錯誤消失。

0

看來你的數據庫有一些boolean類型的列,這意味着它們的值被限制爲truefalse。據廢棄警告,你在你的User模型update_attributesupdate_attributeusers_controller電話設置這些屬性的值到的東西比truefalse(儘管它被強制轉換爲那兩個選項之一反正)不同。

只要你下面的教程,似乎沒有什麼可擔心的:你已經警告說,不同的價值觀的改造,以truefalse的algorighm會在Rails即將發佈改變。

雖然Hartl的教程可能有點過時,但看到這些類型的鑄件仍然有點奇怪。您可能需要仔細檢查您的schema.rb和本書中列出的遷移文件,以確保您的設置絕對正確。