Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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 在正确保存之前,如何使用?_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 在正确保存之前,如何使用?

Ruby on rails 在正确保存之前,如何使用?,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,如果我像这样编写代码,就会出现语法错误。 我该怎么修理 before_save :handle_invitation_code, if: :just_confirmed def just_confirmed? confirmed_at_changed? && confirmed_at_was.nil? end def handle_invitation_code ... end 怎么样 before_save :handle_invitation_code def

如果我像这样编写代码,就会出现语法错误。 我该怎么修理

before_save :handle_invitation_code, if: :just_confirmed

def just_confirmed?
  confirmed_at_changed? && confirmed_at_was.nil?
end

def handle_invitation_code
  ...
end
怎么样

before_save :handle_invitation_code

def just_confirmed?
  confirmed_at_changed? && confirmed_at_was.nil?
end

def handle_invitation_code
  if self.just_confirmed?
  ...
  end
end

或者你必须在确认后把密码传进去?作为保存前的程序。

错误消息是什么?为什么您会告诉我们您有错误,而不告诉我们错误是什么?我们不是通灵者。您是否尝试过在保存之前:处理邀请之前的代码,如果:“self.just\u confirm?”是的,if语句作为字符串,目前在Rails 3.2.11的生产中对我有效,在保存之前:set\u name,如果:'self.name.nil?'@MrYoshiji,我仍然得到以下结果:UsersController的未定义方法'before_save':Class@MrYoshiji我应该在模型中对其进行编码吗?不在控制器中?我仍然为UsersController获取未定义的方法'before\u save':Class no method errorbefore\u save是ActiveRecord回调。。控制器继承自没有回调的ActionController。我想你需要在过滤前过滤。谢谢。我只想在用户通过电子邮件与Desive确认后才执行此方法。你的意思是说刚刚确认?用户模型中存在?很抱歉,答复太晚。否。它存在于用户_controller.rb中