0

我正在關注Ruby on Rails上的Michael Haartl教程。在開發登錄功能之前創建的用戶需要分配一個記憶令牌。對於這一點,我用下面的代碼在控制檯:無法爲導航欄中的每個用戶調用before_save回調

User.all.each { |user| user.save(validate: false) } 

因爲我有:

before save :create remember token 

app/models/user.rb,這應該創建一個記住的道理,但我得到上運行此以下錯誤:

NameError: undefined local variable or method `base64' for  #<User:0x00559aefda7f20> 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activemodel-4.2.4/lib/active_model/attribute_methods.rb:433:in `method_missing' 
from /home/shivani/myapp/app/models/user.rb:13:in `create_remember_token' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:432:in `block in make_lambda' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:164:in `call' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:164:in `block in halting' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:504:in `call' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:504:in `block in call' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:504:in `each' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:504:in `call' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:92:in `__run_callbacks__' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/callbacks.rb:778:in `_run_save_callbacks' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/callbacks.rb:302:in `create_or_update' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/persistence.rb:120:in `save' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/validations.rb:37:in `save' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/attribute_methods/dirty.rb:21:in `save' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-4.2.4/lib/active_record/transactions.rb:286:in `block (2 levels) in save' 
... 14 levels... 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `block in require' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:274:in `require' 
from /home/shivani/myapp/bin/rails:9:in `<top (required)>' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `block in load' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:240:in `load_dependency' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:268:in `load' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' 
from /home/shivani/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require' 

請幫忙! 內容user.rb的:

class User < ActiveRecord::Base 
    has_secure_password 
    before_save { self.email = email.downcase } 
    before_save :create_remember_token 
    private 
    def create_remember_token 
     self.remember_token = SecureRandom.urlsafe Base64 
    end 
    validates :name, presence: true, length: { maximum: 50 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, length: { maximum: 255 }, 
       format: { with: VALID_EMAIL_REGEX }, 
       uniqueness: { case_sensitive: false } 

    validates :password, presence: true, length: { minimum: 6 } 
    end 
+0

你能否分享一下你的'app/models/user.rb'文件的內容? –

+0

類用戶 Shivani

回答

0

app/models/user.rb,改變這種:

def create_remember_token 
    self.remember_token = SecureRandom.urlsafe base64 
end 

這樣:

def create_remember_token 
    self.remember_token = SecureRandom.urlsafe_base64 
end 

您在urlsafe_base54

缺少下劃線( _

來源:http://ruby-doc.org/stdlib-2.2.2/libdoc/securerandom/rdoc/SecureRandom.html#method-c-urlsafe_base64

+1

非常感謝!這是一個非常新人的錯誤 – Shivani

+0

很高興它的工作和歡迎來到!如果有效,請接受我的回答。 :-) –