create multiple rows of record in one model

Issues related to building and deploying Ruby on Rails.
no avatar
veil
 
Posts: 1
Joined: Fri Feb 15, 2008 2:46 pm

create multiple rows of record in one model

by veil » Fri Feb 15, 2008 2:53 pm

hi there.. i have some problem.. im a newbie in rails but i have to do my final year project.
can u edit my code here. i cant save the students mark into the gradebook table.

here's the relation. click on this link (picture) -> http://i250.photobucket.com/albums/gg268/veil007/db.jpg

here's the print screen image of my addgrade.rhtml. when i click "Add Grade" button, it will create new rows (depends on number of students in a class) of record in gradebook table in my database.
--> http://i250.photobucket.com/albums/gg26 ... DEBOOK.jpg

hope anyone willing to share their knowledge

-----------------------------------------------------------------------------
Code: Select all
def egradebook
  @class1s = Class1.find(:all)
  @subjects = Subject.find(:all)
end

def addgrade
  @gradebook = Gradebook.new
  @students = Student.find(:all, :conditions => ["class1_id = ?", params[:class_1][:id]])
  @teachers = Teacher.find(:all)
end

def creategrade
  @gradebook.class1_id = params[:class_1][:id]
  @gradebook.subject_id = params[:subject][:id]
  @students = Student.find(:all, :conditions => ["class1_id = ?", params[:class_1][:id]])
  @gradebook = Gradebook.new(params[:gradebook])
  @teacher = Teacher.find(params[:id])
  ##here is the problem. unsure to add students mark and make the output as multiple of rows in gradebook table.
  if @gradebook.save
    flash[:notice] = 'Gradebook Successfully Added.'
    redirect_to :action => 'index'
  else
    flash[:notice] = 'Please Login to Add Grade'
    render :action => 'login'
  end
end

#-----------------------------------------------------------------------------


#FOR def egradebook
<a name="TemplateInfo"></a>
                <h1>E-Gradebook</h1><br>
                <h3> Select Which Class & Subject You Would Like To Update </h3><br>
<%= start_form_tag :action => 'addgrade' %>
<label for = "class1_id">Select Class</label>
<select name="class_1[id]">
<option value="" selected="selected" >Please Select Class</option>
<%@class1s.each do |class1| %>
<option value="<%=class1.id%>"> <%=class1.classname%> </option>
<%end%>
</select><br> <br>


<label for = "subject_id">Select Subject</label>
<select name="subject[id]">
<option value="" selected="selected" >Please Select Subject</option>
<%@subjects.each do |subject| %>
<option value="<%=subject.id%>"> <%=subject.subjectname%> </option>
<%end%>
</select><br><br>

<%= submit_tag "Submit"%>

<%end_form_tag %><br>

--------------------------------------------

#FOR def addgrade
<a name="TemplateInfo"></a>
                <h1>Update E-Gradebook</h1><br>
                <table width="100%" border="0">
  <tr><%=form_tag :action => 'creategrade', :id => @gradebook%>
    <td width="22%"><strong>Class:</strong> <%=params[:class_1][:id]%></td>
    <td colspan="2"><strong>Subject:</strong> <%=params[:subject][:id]%> </td>
  </tr>
  <tr>
    <td><strong>Semester:</strong> <%= text_field 'gradebook', 'semester' , :size => 3 %></td>
    <td width="20%"><strong>Year:</strong> <%= text_field 'gradebook', 'year' , :size => 5 %></td>
    <td width="58%"><label for = "teacher_id"><strong> Select Teacher: </strong></label>
<select name="gradebook[teacher_id]">
<option value="" selected="selected" >Please Select Teacher Name</option>
<%@teachers.each do |teacher| %>
<option value="<%=teacher.id%>"> [<%=teacher.user.id%>] <%=teacher.user.firstname%> <%=teacher.user.lastname%> </option>
<%end%>
</select></td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="2">&nbsp;</td>
  </tr>
</table>
<br>
<table width="100%" border="0">
  <tr>
    <td width="76%"><strong>Student ID & Name </strong></td>
    <td width="12%"><div align="center"><strong>Mark</strong></div></td>
    <td width="12%"><div align="center"><strong>Grade</strong></div></td>
  </tr>
  <tr><%@students.each do |student|%>
    <td>[<%=student.user.id%>] <%=student.user.firstname%> <%=student.user.lastname%></td>
    <td><div align="center"> <%= text_field 'gradebook', 'mark' , :size => 3 %></div></td>
    <td><div align="center"><%= text_field 'gradebook', 'grade' , :size => 3 %></div></td>
  </tr><%end%>
</table>
<div align="center"><br>
    <%= submit_tag "Add Grade" %> <%= end_form_tag %>

def creategrade is the action to save the data into the database

Return to Ruby on Rails

cron