[英]Help with rails nested forms
我有這三個模型。
學生。 評估。 成績(student_id,evaluatioin_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
關於您的評論,我想您想使用Ryan Bates制造的nested_form_for寶石。 它可以幫助您非常高效地在表單中添加/刪除動態嵌套的屬性。 您可以像這樣使用它:
<% 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.