Today’s progress doesn’t finish the user interface, but takes it up until storage in the database: the notes form field is fully displayed and functional, and some ground work is laid for saving the data tomorrow.
As before the HTML Special Character Converter is used, and the main resource I found today web-find was the InVisible – Ruby on Rails Reference.
First, let’s clear up app/views/students/_notes_view.rhtml
Update it like so:
<h1>Note-Taking Matrix</h1>
<%= form_remote_tag(:update => "notetaking_display",
:url => { :action => :save_student_notes },
:position => "top" ) %>
<table border="1">
<tr>
<td> </td>
<% for notes_record in @notes_records %>
<td><%= h(notes_record.name) %></td>
<% end %>
</tr>
<% for notes_field in @notes_fields %>
<tr>
<td><%= notes_field.name %></td>
<% for notes_record in @notes_records %>
<td>
<%= text_area h(notes_field.name), h(notes_record.name), :cols => 20, :rows => 8 %>
</td>
<% end %>
</tr>
<% end %>
</table>
<%= submit_tag "Save Notes" %>
<%= end_form_tag %>
<div id="notetaking_display" name="notetaking_display"></div>
That works pretty well. But we forgot to have a place for the student to log in, and we don’t have a way to carry over the conditions or experiments to later processing. So let’s generate a student model
ruby script/generate model student
The generated file will be 012_create_students.rb. Make it like so:
class CreateStudents < ActiveRecord::Migration
def self.up
create_table :students do |t|
t.column :name, :string
t.column :condition_id, :integer
end
end
def self.down
drop_table :students
end
end
Then let’s create a notes model
ruby script/generate model note
And then 013_create_notes.rb is:
class CreateNotes < ActiveRecord::Migration
def self.up
create_table :notes do |t|
t.column :student_id, :integer
t.column :notetext, :string
end
end
def self.down
drop_table :notes
end
end
Then rake db:migrate
Now we’ll add make the student model a session variable so that information can be rememberd more easily. So using information from Agile Web Development with Rails — 2nd Edition (107), add the following function to the student controller
Update student.rb like so:
class Student < ActiveRecord::Base
attr_reader :condition_id, :name
belongs_to :condition
has_many :notes
end
And update note.rb like so:
class Note < ActiveRecord::Base
attr_reader :student_id, :note_text
belongs_to :student
end
Now that that’s done, it’s too hard to figure out how to actually keep an object in memory. So for now we’ll just just edit some functions to store information in the session variable. First, go to select_notes_from_condition in students_controller.rb and add
session[:condition_id] = selected_condition_id
at the very bottom. Then write the following functions
def save_student_notes
render_text session[:student].condition_id
end’
So we completed the form to allow students to input their notes. But how to add that information to the database?
Check in tomorrow!