Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/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 如何建立一对一关系?_Ruby_Ruby On Rails 4 - Fatal编程技术网

Ruby 如何建立一对一关系?

Ruby 如何建立一对一关系?,ruby,ruby-on-rails-4,Ruby,Ruby On Rails 4,我不熟悉rails。 请帮助我如何创建rails一对一关系。 我有两张桌子,abc和pqr。 在模型中,我声明在abc模型中有一个:pqr,在pqr模型中属于:abc。我不知道如何为“pqr”编写视图和控制器。假设我们有两个表:person和dog 首先,您需要在dog表中创建一个person\u id。 在模型中 然后添加has_on:dogin person.rb 并且在dog.rb中属于:person 在控制器中 创建属于人的狗。你需要先创建一个人 @person = Person.new

我不熟悉rails。 请帮助我如何创建rails一对一关系。 我有两张桌子,abc和pqr。
在模型中,我声明在abc模型中
有一个:pqr
,在pqr模型中
属于:abc
。我不知道如何为“pqr”编写视图和控制器。假设我们有两个表:
person
dog

首先,您需要在
dog
表中创建一个
person\u id
。 在模型中 然后添加
has_on:dog
in person.rb 并且
在dog.rb中属于:person

在控制器中 创建属于
人的
。你需要先创建一个人

@person = Person.new(params[:person])
@person.save
然后创造他的狗

@dog = Person.create_dog(params[:dog])
@dog.save
要访问某人的狗,只需键入

@person.dog
你可以用

bin/rails generate controller Pqr hello
它将为视图生成一个控制器文件、一个视图文件、一个功能测试文件和一个助手,。有关更多信息,请查看

您可以在controller
pqr\u controller.rb

class PqrController < ApplicationController
  def hello
    @content = "Hello World"
  end
end
class PqrController < ApplicationController
  def hello
    @content = "Hello World"
  end
end
class PqrController < ApplicationController
  def hello
    @content = "Hello World"
  end
  def show
    @contents = "Test"
  end
end