Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.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 Rails 5:如何将设备指向其他设备?_Ruby On Rails_Ruby On Rails 5.2 - Fatal编程技术网

Ruby on rails Rails 5:如何将设备指向其他设备?

Ruby on rails Rails 5:如何将设备指向其他设备?,ruby-on-rails,ruby-on-rails-5.2,Ruby On Rails,Ruby On Rails 5.2,I三种模型注释,用户和项目项目和注释需要指向其他对象才能有效。例如,注释需要指向作者(用户)和项目 关联的装置文件如下所示: # comments.yml test_comment: author: users(:test_user) project: projects(:test_project) # users.yml test_user: name: 'test user' # projects.yml test_project: name: 'Test' desc

I三种模型
注释
用户
项目
<代码>项目和注释需要指向其他对象才能有效。例如,注释需要指向作者(用户)和项目

关联的装置文件如下所示:

# comments.yml
test_comment:
  author: users(:test_user)
  project: projects(:test_project)

# users.yml
test_user:
  name: 'test user'

# projects.yml
test_project:
  name: 'Test'
  description: 'This is a test'
  owner: users(:test_user)
然而,我发现我的设备可能设置不正确。如果我尝试保存注释,Rails将返回
false

assert_equal true, comments(:test_comment)
#=> false
我可以看到项目和作者有外键:

=> #<Comment:0x00007f9b1661f3d8
 id: 137725605,
 body: "",
 project_id: 745075726,
 author_id: "31ceee04-5307-5059-91db-0dc2068a780c",
 created_at: Fri, 22 Feb 2019 13:17:58 UTC +00:00,
 updated_at: Fri, 22 Feb 2019 13:17:58 UTC +00:00>
我希望其中一个将返回
用户(:test\u user)
,另一个将返回
项目(:test\u project)
。我想也许我需要在yaml中使用ERB:

test_comment:
  author: <%= users(:test_user) %>
  project: <%= projects(:test_project) %>
test\u注释:
作者:
项目:
但当我运行测试时,结果是一连串的错误:

NoMethodError: undefined method `users' for #<#<Class:0x00007f9b17692ff8>:0x00007f9b17692dc8>
NoMethodError:#
我需要做什么才能将装置指向其他装置?能做到吗?我做错了什么?

在中,您可以看到不需要
用户(:test\u user)
来引用其他对象。相反,您只需编写
test\u user

# comments.yml
test_comment:
  author: test_user
  project: test_project

# users.yml
test_user:
  name: 'test user'

# projects.yml
test_project:
  name: 'Test'
  description: 'This is a test'
  owner: test_user
希望这有帮助

# comments.yml
test_comment:
  author: test_user
  project: test_project

# users.yml
test_user:
  name: 'test user'

# projects.yml
test_project:
  name: 'Test'
  description: 'This is a test'
  owner: test_user