Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.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 Rails3.2为nil:NilClass提供了一个build:undefined方法序列化的属性_Ruby On Rails_Ruby On Rails 3.2_Associations - Fatal编程技术网

Ruby on rails Rails3.2为nil:NilClass提供了一个build:undefined方法序列化的属性

Ruby on rails Rails3.2为nil:NilClass提供了一个build:undefined方法序列化的属性,ruby-on-rails,ruby-on-rails-3.2,associations,Ruby On Rails,Ruby On Rails 3.2,Associations,我使用的是Rails 3.2。我正在尝试为has_-one关系构建一个对象(每个驱动程序都有一个cdl_驾驶执照)。我以前做过很多类似的工作,效果非常好。在这种特殊情况下,我不断遇到以下错误: undefined method `serialized_attributes' for nil:NilClass 这是我的相关代码: driver.rb has_one :cdl_driving_license, dependent: :destroy accepts_nested_attribute

我使用的是Rails 3.2。我正在尝试为has_-one关系构建一个对象(每个驱动程序都有一个cdl_驾驶执照)。我以前做过很多类似的工作,效果非常好。在这种特殊情况下,我不断遇到以下错误:

undefined method `serialized_attributes' for nil:NilClass
这是我的相关代码:

driver.rb

has_one :cdl_driving_license, dependent: :destroy
accepts_nested_attributes_for :cdl_driving_license, :allow_destroy => true
cdl_驾驶执照.rb

belongs_to  :driver 
驱动程序控制器.rb

def new
 @driver = Driver.new
 @driver.build_cdl_driving_license
 #more code...
end

知道是什么导致了这个错误吗

在您的新方法中,您是否尝试执行以下操作:
@driver.cdl\u driving\u license.build(这里是cdl driving license的参数)?

解决了这个问题。我试图构建的记录属于一个表,该表包含一个保留名称的列(我有一个名为“class”的列)。哼

不。据我所知,如果关联是使用has_one定义的,那么我们使用“build_association”函数。对于很多人来说,它应该是“关联.构建”。在我的例子中,我有一个has_-one关联。我刚刚将我的关联更改为has_-many,并使用“@driver.cdl_driving_licenses.build”。通过这些更改,我仍然从“build”行得到相同的错误“undefined method serialized attributes for nil:NilClass”。。