Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么“before_save”在Rails 4中不起作用?_Ruby On Rails_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 为什么“before_save”在Rails 4中不起作用?

Ruby on rails 为什么“before_save”在Rails 4中不起作用?,ruby-on-rails,ruby,ruby-on-rails-4,Ruby On Rails,Ruby,Ruby On Rails 4,当我尝试u=User.new后跟u.save时,不会触发before\u save方法 这是我的用户型号: class User < ActiveRecord::Base attr_accessor :password before_save :encrypt_password def encrypt_password puts "Its getting called" end end class用户

当我尝试
u=User.new
后跟
u.save
时,不会触发
before\u save
方法

这是我的
用户
型号:

class User < ActiveRecord::Base
  attr_accessor :password
  before_save :encrypt_password

  def encrypt_password
    puts "Its getting called"
  end
end
class用户

回调方法需要
保护
私有


从这里开始:在
回调类型

部分下,如果您只是测试它是否被触发,您应该使用
Rails.logger.debug
。正常情况下,这应该是可行的。@nathanvda:日志文件在哪里?这是一个诡计问题吗?在你的rails文件夹中应该有一个
log
文件夹,它应该包含一个
development.log
文件(如果你在开发模式下运行)。@nathanvda:rails.log.info“Hello”它在development.log中不打印任何内容。你的意思是
rails.logger.info
对吗?我使用的rails 4在保存之前没有任何问题。这是您展示的完整类吗?puts“Hello”仍然没有将任何内容打印到rails控制台。在控制台中打印的任何特定方法。它不会打印到控制台;它将出现在运行服务器的shell上。或者,如果您的服务器运行的是apache或nginx之类的程序,则应该在各自的日志文件中。@Shane请尝试logger.info或Rails::logger.info进行调试