Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.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_Rest_Nested Forms - Fatal编程技术网

Ruby on rails rails嵌套表单的帮助

Ruby on rails rails嵌套表单的帮助,ruby-on-rails,rest,nested-forms,Ruby On Rails,Rest,Nested Forms,我有这三种型号 学生。 评价 成绩(学生id、评估id、价值) 我想做一个表格,这样用户就可以为每个学生的评估设置分数 我想保持这是尽可能干净(和休息) 我愿意接受任何关于如何做到这一点的建议 请帮忙。看看这些 我想你有以下联系: 评估: class Evaluation < ActiveRecord:Base has_many :grades accepts_nested_attributes_for :grades end class Student < Active

我有这三种型号

学生。 评价 成绩(学生id、评估id、价值)

我想做一个表格,这样用户就可以为每个学生的评估设置分数

我想保持这是尽可能干净(和休息)

我愿意接受任何关于如何做到这一点的建议

请帮忙。

看看这些


我想你有以下联系:

评估:

class Evaluation < ActiveRecord:Base
  has_many :grades
  accepts_nested_attributes_for :grades
end
class Student < ActiveRecord::Base
  has_many :grades
end
class Grade < ActiveRecord::Base
   belongs_to :student
   blongs_to  :evaluation
end
类评估
学生:

class Evaluation < ActiveRecord:Base
  has_many :grades
  accepts_nested_attributes_for :grades
end
class Student < ActiveRecord::Base
  has_many :grades
end
class Grade < ActiveRecord::Base
   belongs_to :student
   blongs_to  :evaluation
end
class-Student
等级:

class Evaluation < ActiveRecord:Base
  has_many :grades
  accepts_nested_attributes_for :grades
end
class Student < ActiveRecord::Base
  has_many :grades
end
class Grade < ActiveRecord::Base
   belongs_to :student
   blongs_to  :evaluation
end
等级
关于你的评论,我认为你想使用瑞安·贝茨制作的宝石。它可以帮助您非常高效地在表单中添加/删除动态嵌套的属性。您可以这样使用它:

    <% nested_form_for @evaluation do |f| %>
      <% f.fields_for :grades do |gf| %>
        <%= gf.label 'Username of the student' %>
        <%= gf.collection_select(:student, :student_id, Student.All, :id, :username) %>    <br/>
        <%= gf.label :value %>
        <%= gf.text_field :value %> <br/>
        <%= gf.link_to_remove 'delete' %> <br/>
      <% end %>
      <%= f.link_to_add 'Add grade', :grades %> <br/>
      <%= f.submit %>
    <% end %>






告诉我它是否适合你的需要。前几天我一直在使用这个宝石。

我最后做的是

同级模型

scope :for_student,lambda{|student, evaluation| where("student_id"=>student.id).where("evaluation_id"=>evaluation.id)}
在EvaluationsController中

def assign_grades])
    @evaluation = Evaluation.find(params[:id])
    @students = Student.all  
    @students.each do |s|
      grade = Grade.for_student(s,@evaluation)
      if !grade.exists?  
       @evaluation.grades.build(:value=> 0,:student_id=>s.id)       
      end   
    end
  end

  def save_grades
    @evaluation = Evaluation.find(params[:id])
    @evaluation.update_attributes(params[:evaluation])

    redirect_to [@evaluation.batch_subject, @evaluation]

  end
鉴于

<table class="medium">      
<thead>
   <tr>
   <th>name</th>
   <th>Grade</th>
   </tr>
</thead>
<tbody>
   <%=f.fields_for :grades do |g|%>
    <tr>
      <td><%=g.label :value,g.object.student.full_name%> <%= g.hidden_field :student_id%></td>
<td><%=g.text_field :value%></td>
</tr>
<%end%>
</tbody>
</table>

名称
等级

您是否设置了这些模型之间的关联?如果是,请提供每个型号的代码。是,关联设置为。。。我想要的是关于表单/视图处理的建议。。。我知道如何接受嵌套属性,但我认为在评估的新建/创建/编辑中添加分数一点也不优雅。。。但是该表单没有在任何地方显示学生的姓名。。。我如何在学生成绩的输入文本字段前显示学生的姓名?请看我的编辑。我在一个选择框中显示了学生的名字。谢谢你的快速回复。。问题是每个学生每次评估都必须有一个分数。。我实际上不需要选择框。我只想要一个所有学生的列表,每个学生旁边都有一个文本字段。。。好的,我知道你想要什么了(尽管我的建议允许你动态地添加一对(学生,年级),这样你就可以为你的每个学生都这样做)。让我想想:)。谢谢,老兄,我很感激。。。我已经考虑了几天了,我似乎找不到一个优雅的解决方案