Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/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 更新父项时未设置父项id_Ruby On Rails_Ruby_Postgresql_Ruby On Rails 4_Activerecord - Fatal编程技术网

Ruby on rails 更新父项时未设置父项id

Ruby on rails 更新父项时未设置父项id,ruby-on-rails,ruby,postgresql,ruby-on-rails-4,activerecord,Ruby On Rails,Ruby,Postgresql,Ruby On Rails 4,Activerecord,我有两种型号 class Parent < ActiveRecord::Base has_one :child accepts_nested_attributes_for_child :child end 当我在自定义\u验证中检查父\u id时,它会返回我nil,并为parent.attribute\u one获取nil类错误,因为parent为nil我在这里做错了什么。 这是回溯 NoMethodError (undefined method `attribute_one'

我有两种型号

class Parent < ActiveRecord::Base
  has_one :child
  accepts_nested_attributes_for_child :child
end
当我在自定义\u验证中检查父\u id时,它会返回我nil,并为parent.attribute\u one获取nil类错误,因为parent为nil我在这里做错了什么。 这是回溯

NoMethodError (undefined method `attribute_one' for nil:NilClass):
  app/models/child.rb:8:in `custom_validation'
  app/controllers/parents_controller.rb:12:in `update'


  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.6ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.6ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.8ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (19.6ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/_markup.html (0.4ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/style.css within layouts/inlined_string (0.5ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/_inner_console_markup.html within layouts/inlined_string (0.4ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/_prompt_box_markup.html within layouts/inlined_string (0.3ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/console.js within layouts/javascript (17.9ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/main.js within layouts/javascript (0.3ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/error_page.js within layouts/javascript (0.4ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/index.html (34.4ms)

这是一个愚蠢的错误,我忘了为child在强参数中添加id

def trasnfer_params
  params.require(:parent).permit(:attribute_one, :attribute_two, child_attributes:[:id, :parent_id, :attribute_one]) #added id in child_attributes
end

已解决。

如果self.attribute\u one>self.parent.attribute\u one我尝试了self.parent或parent将返回nil,除非未设置parent\u id。请在此处使用并发布异常堆栈跟踪。我们必须以某种方式检查记录保存是否成功。@mudasobwa我已经更新了这个问题
NoMethodError (undefined method `attribute_one' for nil:NilClass):
  app/models/child.rb:8:in `custom_validation'
  app/controllers/parents_controller.rb:12:in `update'


  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (4.6ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_trace.text.erb (0.6ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.text.erb (0.8ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/actionpack-4.2.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.text.erb (19.6ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/_markup.html (0.4ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/style.css within layouts/inlined_string (0.5ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/_inner_console_markup.html within layouts/inlined_string (0.4ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/_prompt_box_markup.html within layouts/inlined_string (0.3ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/console.js within layouts/javascript (17.9ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/main.js within layouts/javascript (0.3ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/error_page.js within layouts/javascript (0.4ms)
  Rendered vendor/bundle/ruby/2.2.0/gems/web-console-2.1.1/lib/web_console/templates/index.html (34.4ms)
def trasnfer_params
  params.require(:parent).permit(:attribute_one, :attribute_two, child_attributes:[:id, :parent_id, :attribute_one]) #added id in child_attributes
end