Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.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
Ruby on rails Rails与预填充视图有很多关系_Ruby On Rails_Ruby On Rails 4_Nested Forms_Cocoon Gem - Fatal编程技术网

Ruby on rails Rails与预填充视图有很多关系

Ruby on rails Rails与预填充视图有很多关系,ruby-on-rails,ruby-on-rails-4,nested-forms,cocoon-gem,Ruby On Rails,Ruby On Rails 4,Nested Forms,Cocoon Gem,我有一个非常基本的Rails 4应用程序,我正在使用Cocoon的嵌套表单来管理有很多…:通过模型关联 class Student < ActiveRecord::Base has_many :evaluations has_many :assessments, through: :evaluations # ... etc end class Evaluation < ActiveRecord::Base belongs_to :student belong

我有一个非常基本的Rails 4应用程序,我正在使用Cocoon的嵌套表单来管理
有很多…:通过
模型关联

class Student < ActiveRecord::Base
  has_many :evaluations
  has_many :assessments, through: :evaluations

  # ... etc
end

class Evaluation < ActiveRecord::Base
  belongs_to :student
  belongs_to :assessment

  # ... etc
end

class Assessment < ActiveRecord::Base
  has_many :evaluations
  has_many :students, through: :evaluations

  accepts_nested_attributes_for :evaluation, reject_if: :all_blank
  # ... etc
end
我在中看到这是以某种方式生成的,但我无法理解它如何与Rails引擎一起工作,以便在没有
ID
的情况下将其生成新记录

我应该使用什么算法(或者应该遵循什么规则)来填写上面的
id
,以创建新记录?

“?

在你的情人身上从来都不是好兆头

在Cocoon中,我看到请求在
id
的空间中有一些数字

ID
只不过是Rails创建的
数组的
字段中的下一个ID。这不是您记录的
id
(下面将详细说明)


根据您的设置,以下是我要做的:

#app/models/student.rb
class Student < ActiveRecord::Base
   has_many :evaluations
   has_many :assessments, through: :evaluations
end

#app/models/evaluation.rb
class Evaluation < ActiveRecord::Base
  belongs_to :student
  belongs_to :assessment
end

#app/models/assessment.rb
class Assessment < ActiveRecord::Base
   has_many :evaluations
   has_many :students, through: :evaluations
   accepts_nested_attributes_for :evaluations, reject_if: :all_blank
end
#app/models/student.rb
班级学生
这将允许您执行以下操作:

#app/controllers/assessments_controller.rb
class AssessmentsController < ApplicationController
   def new
      @assessment = Assessment.new
      @students = Student.all
      @students.each do
         @assessment.evaluations.build
      end
   end
end
#app/controllers/assessments_controller.rb
类评估控制器<应用程序控制器
def新
@assessment=assessment.new
@学生
@学生们,每个人都有
@评估、评价、构建
结束
结束
结束
允许您:

#app/views/assessments/new.html.erb
<%= form_for @assessment do |f| %>
   <%= f.fields_for :evaluations, @students do |e| %>
       <%= e.hidden_field :student_id %>
       <%= e.text_field :grade %>
   <% end %> 
   <%= f.submit %>
<% end %>
#app/views/assessments/new.html.erb
据我所知,这将提供您需要的功能

请记住,每个
评估
都可以与现有的
学生
连接,这意味着如果您拉取
@students=Student.all
,它将相应地填充
字段

如果您想通过表单添加新的
学生
,这是一个稍微不同的游戏


您还应该清楚的了解

你似乎是一个经验丰富的开发人员,所以我将切入重点——Cocoon是前端,而你问的是后端

具体来说,Cocoon旨在让您能够为表单中的
相关字段添加大量的
字段。本文对此进行了讨论

从技术上讲,Cocoon只是为表单的
记录创建新的
字段的一种方法。仅当您希望动态“添加”字段时才需要它(RailsCast会告诉您更多)


因此,如果您只想拥有一个关联数据字段的“静态”数组(我想这就是您要问的),那么您就可以在
Max
和我的答案中使用as submitted。

多亏了@rich peck,我才能够准确地知道我想要做什么。我认为他的答案是可以接受的,因为这基本上是我自己的答案

assessments/new.html.haml
(只是原始的,没有花哨的格式)

assessments\u controller.rb

def new
  @assessment = Assessment.new
  @students = Student.all
  @students.each do |student|
    @assessment.evaluations.build(student: student)
  end
end

请包括相关的controller@max除了白名单参数和调用
@assessment.new(assessment\u params)
然后调用
@assessment.save
是的,但是你通常会在控制器的新动作中建立相关的记录——这样我们就不必猜测了。我想避免手动建立这些东西。如果我通过视图创建新记录(为每个学生添加新记录,这是我想要自动化的),它将按照上面的方式构造它们,并且除了使用
new
和使用嵌套表单以及
接受
的嵌套属性之外,我不需要任何特殊的控制器逻辑。我只需将白名单中的参数传递给
new
方法,Rails就会创建父
assessment
和相关的
evaluation
对象。非常感谢@rich peck。是的,
??
被我替换了,特别是在这个问题上,所以他们没有在请求中通过:)(按enter键太快)我知道Cocoon是所有前端,但我不确定是什么逻辑创建了它通过请求传回的ID。只要它们是独一无二的,它们就无关紧要吗?或者它们必须是与已经存在的
求值
的ID不对应的任何东西吗?ID是
时间。现在。to_i
;)我想他们可能是!这完全回答了我的问题。非常感谢你的时间。是的,时间到了-
= form_for @assessment do |f|
  = f.fields_for :evaluations do |ff|
    .meaningless-div
      = ff.object.student.name
      = ff.hidden_field :student_id, value: ff.object.student_id
      = ff.label :comment
      = ff.text_field :comment
      %br/
def new
  @assessment = Assessment.new
  @students = Student.all
  @students.each do |student|
    @assessment.evaluations.build(student: student)
  end
end