NoMethodError身份验证 - RailsTutorial

时间:2016-07-12 16:18:13

标签: ruby-on-rails railstutorial.org

我第二次关注Rails教程,我遇到了第一次尝试时没有遇到的问题。

此错误发生在控制台中。

我可以使用find_by_id,find_by_email方法,但不能使用身份验证方法。

什么宝石提供这种方法? bycript?

这是错误消息。

irb(main):003:0> User.find_by_id("4")
  User Load (0.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 4], ["LIMIT", 1]]
=> #<User id: 4, name: "Michael Hartl", email: "mhartl@example.com", created_at: "2016-07-12 16:08:19", updated_at: "2016-07-12 16:08:19", password_digest: "$2a$10$t4MhC/fb1gcnjygY1YPeaubxSBoY7tw3WNysg4TTGC1...">
irb(main):004:0> User.find_by_id("3")
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 3], ["LIMIT", 1]]
=> nil
irb(main):005:0> User.authenticate("oefheihfie")
NoMethodError: undefined method `authenticate' for #<Class:0x007f7f770e1ed0>
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-5.0.0/lib/active_record/dynamic_matchers.rb:21:in `method_missing'
    from (irb):5
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands/console.rb:65:in `start'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands/console_helper.rb:9:in `start'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:78:in `console'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands/commands_tasks.rb:49:in `run_command!'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-5.0.0/lib/rails/commands.rb:18:in `<top (required)>'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `block in require'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:293:in `require'
    from /home/budi/gogo_app/bin/rails:9:in `<top (required)>'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `block in load'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:259:in `load_dependency'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-5.0.0/lib/active_support/dependencies.rb:287:in `load'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.1/lib/spring/commands/rails.rb:6:in `call'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.1/lib/spring/command_wrapper.rb:38:in `call'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.1/lib/spring/application.rb:191:in `block in serve'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.1/lib/spring/application.rb:161:in `fork'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.1/lib/spring/application.rb:161:in `serve'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.1/lib/spring/application.rb:131:in `block in run'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.1/lib/spring/application.rb:125:in `loop'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.1/lib/spring/application.rb:125:in `run'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/spring-1.7.1/lib/spring/application/boot.rb:19:in `<top (required)>'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /home/budi/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from -e:1:in `<main>'irb(main):006:0> ^C

1 个答案:

答案 0 :(得分:0)

解决!

我忘了将实例分配到变量中。 分配后,我可以对该变量使用authenticate方法。