未定义的局部变量或方法

我在

我正在遵循此代码https://stackoverflow.com/a/17886089/692622,这是我的client.rb模型文件和client_controller.rb文件

# app/models/client.rb
before_create :add_unsubscribe_hash

private

def add_unsubscribe_hash
    self.unsubscribe_hash = SecureRandom.hex
end

# app/controllers/clients_controller.rb
def unsubscribe
    client = Client.find_by_unsubscribe_hash(params[:unsubscribe_hash])
    client.update_attribute(:subscription, false)
end

但是当我尝试通过/ clients / new添加客户端(我在控制器文件中也有所有7种方法)时,出现了错误

undefined local variable or method `add_unsubscribe_hash'

将客户端保存在create方法中时出现错误

respond_to do |format|
  if @client.save

任何想法都错了,因为一切看起来都很好

编辑-我已经在pastebin http://pastebin.com/jkegLsaE中添加了模型代码

玉米

请注意,在第40行的引擎收录,你已经开了一个foreach循环,应该在42行终止,但不会相反,foreach循环包含整个add_unsubscribe_hash函数声明,因此:before_create回调无法调用它

通过结束应在其中关闭函数的循环来解决此问题(并确保删除end文件末尾的无关标签):

# app/models/contact.rb
def self.import(file)
    CSV.foreach(file.path, headers: true) do |row|
        Contact.create! row.to_hash
    end
end

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章