帮助处理 Rails 嵌套表单

发布于 2024-10-22 19:12:09 字数 172 浏览 1 评论 0原文

我有这三个型号。

学生。 评估。 成绩(student_id,evaluatioin_id,value)

我想制作一个表格,以便用户可以为评估的每个学生设置成绩...

我想保持尽可能干净(和安静)...

我开放有关如何完成此操作的任何建议。

请帮忙。

I have these three models.

Student.
Evaluation.
Grade (student_id, evaluatioin_id, value)

I want to make a form so that a User can set the grade for each student of a evaluation...

I want to keep this is as clean as possible (and restful)...

Im open to any suggestions on how to get this done.

Please Help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流云如水 2024-10-29 19:12:09

我想您有以下关联:

评估:

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 由 Ryan Bates 制作的 gem。它可以帮助您非常有效地在表单中添加/删除动态嵌套属性。您可以这样使用它:

    <% 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 %>

告诉我它是否有效/适合您的需求。前几天我经常使用这个宝石。

I suppose you have the following associations:

Evaluation:

class Evaluation < ActiveRecord:Base
  has_many :grades
  accepts_nested_attributes_for :grades
end

Student:

class Student < ActiveRecord::Base
  has_many :grades
end

Grade:

class Grade < ActiveRecord::Base
   belongs_to :student
   blongs_to  :evaluation
end

Regarding your comment I think you want to use the nested_form_for gem made by Ryan Bates. It helps you add/delete dynamically nested attributes in your form, very efficiently. You can use it like this:

    <% 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 %>

Tell me if it works/fits your needs. I have been using this gem a lot these previous days.

糖粟与秋泊 2024-10-29 19:12:09

我最终做的是..

中的等级模型中

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>

What I ended up doing was..

In Grade Model

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

In 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

In View

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文