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
Ruby on rails 4 导轨中的嵌套形式__Ruby On Rails 4_Nested Form For - Fatal编程技术网

Ruby on rails 4 导轨中的嵌套形式_

Ruby on rails 4 导轨中的嵌套形式_,ruby-on-rails-4,nested-form-for,Ruby On Rails 4,Nested Form For,我有测试模型和用户模型测试模型有许多用户。测试控制器如下所示 class TestController def create Test.create(testparams) end private def testparams params.require(:test).permit(:test_name,user_attributes:[:user_name]) end end 在上面的代码中,将创建新的测试。我想为现有测试创建新用户。

我有
测试
模型和
用户
模型<代码>测试模型有许多
用户
。测试控制器如下所示

class TestController
   def create
      Test.create(testparams)
   end

   private
   def testparams
      params.require(:test).permit(:test_name,user_attributes:[:user_name])
   end
end

在上面的代码中,将创建新的测试。我想为现有测试创建新用户。如何做到这一点???

您应该能够应用相同的原则。下面是一个基本框架,您必须根据自己的需求进行修改

测试模型

accepts_nested_attributes_for :users, allow_destroy: true
测试控制器

def edit
  @test = Test.find(params["id"]
  @test.users.build
end

def update
  @test = Test.find(params["id"]
  @test.update(testparams)
end
测试视图

<%= form_for @test do |f| %>
  <%= f.text_field :test_name %>

  <%= f.fields_for :users do |uf| %>
    <%= uf.text_field :user_name %>
  <% end %>
<% end %>

您应该能够应用相同的原则。下面是一个基本框架,您必须根据自己的需求进行修改

测试模型

accepts_nested_attributes_for :users, allow_destroy: true
测试控制器

def edit
  @test = Test.find(params["id"]
  @test.users.build
end

def update
  @test = Test.find(params["id"]
  @test.update(testparams)
end
测试视图

<%= form_for @test do |f| %>
  <%= f.text_field :test_name %>

  <%= f.fields_for :users do |uf| %>
    <%= uf.text_field :user_name %>
  <% end %>
<% end %>


如果你在谷歌上搜索rails嵌套表单,你会发现大量的资源来帮助你入门。也许从rails指南开始吧。我的问题是关于现有对象(在本例中为测试对象)的嵌套表单_。如果您在谷歌上搜索rails嵌套表单,您将找到大量资源来开始。也许从rails指南开始,.我的问题是关于现有对象的嵌套形式_(在本例中为测试对象)