create multiple rows of record in one model

Issues related to building and deploying Ruby on Rails.
no avatar
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) ->

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.
--> ... DEBOOK.jpg

hope anyone willing to share their knowledge

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

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

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])
  @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.
    flash[:notice] = 'Gradebook Successfully Added.'
    redirect_to :action => 'index'
    flash[:notice] = 'Please Login to Add Grade'
    render :action => 'login'


#FOR def egradebook
<a name="TemplateInfo"></a>
                <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.classname%> </option>
</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.subjectname%> </option>

<%= 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>
    <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.user.firstname%> <%=teacher.user.lastname%> </option>
    <td colspan="2">&nbsp;</td>
<table width="100%" border="0">
    <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><%@students.each do |student|%>
    <td>[<>] <%=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>
<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