简体   繁体   English

Rails错误:未定义的方法`private_method_defined?' 为零:NilClass

[英]Rails Error: undefined method `private_method_defined?' for nil:NilClass

I'm trying to create and save an object, but I keep getting the error: 我正在尝试创建并保存对象,但我一直收到错误:

undefined method `private_method_defined?' for nil:NilClass

I wrote some simple code to work out the bug. 我写了一些简单的代码来解决这个bug。 In the controller, the code looks like this: 在控制器中,代码如下所示:

def testtutor
     @t = Tutor.new
     @t.fname = 'Test'
end

The model is mostly blank: 该模型大多是空白的:

class Tutor < ActiveRecord::Base
  belongs_to :branch
end

The error occurs at: 错误发生在:

@t.fname = 'Test'

Any idea what could be going on? 知道会发生什么吗? I have created and migrated a "tutors" table, and "fname" is one of the fields. 我创建并迁移了“教师”表,“fname”是其中一个字段。

Thanks for the help! 谢谢您的帮助!

This error occurs if any database column names conflict with rails. 如果任何数据库列名与rails冲突,则会发生此错误。

I faced the same issue when I had a column called "class". 当我有一个名为“class”的专栏时,我遇到了同样的问题。

Renaming it will fix the issue. 重命名它将解决问题。

I'd go into the console and try creating an entry there. 我会进入控制台并尝试在那里创建一个条目。 t = Tutor.new

Then type 然后输入

t.valid?

If it says false type 如果它说假类型

t.errors.full_messages and it will print out exactly what the issues are t.errors.full_messages ,它将准确打印t.errors.full_messages所在

Hope that helps 希望有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM