Ruby Source Code: fedit.rb
#!/usr/bin/ruby
require 'cgi'
require './common'
#----------------------------------------------------------
# The Web Language Project
# Mark Brautigam
# May-June 2015
# http://www.mixed-up.com/markb/
#----------------------------------------------------------
cgi = CGI.new
id = cgi ['id']
if (id == '')
print ("Location: fread.rb\n\n");
end
f = File.new('../data/courses.txt', 'r')
courses = f.readlines()
ncourses = courses.count
id = id.to_i
if (id < 0 or id > ncourses-1)
print ("Location: fread.rb\n\n");
end
coursedata = courses[id].split("|")
puts cgi.header
headers1()
headers2()
print sidebarX("ruby", "fedit", "rb")
#print coursedata
print %q(
<div id="content">
<h2>Ruby: Edit the data file</h2>
<form name='coursesform' action='fedit.rb' method='POST'>
<fieldset>
<legend>Edit a course</legend>
<table>
)
printf("<tr><td>College:</td><td><input type='text' name='college' value='%s' /></td></tr>", coursedata[0]);
printf("<tr><td>Department:</td><td><input type='text' name='dept' value='%s' /></td></tr>", coursedata[1]);
printf("<tr><td>Course #:</td><td><input type='text' name='course' value='%s' /></td></tr>", coursedata[2]);
printf("<tr><td>Course name:</td><td><input type='text' name='desc' value='%s' /></td></tr>", coursedata[3]);
printf("<input type='hidden' name='id' value='%d' />", id);
print %q(
<tr><td></td><td><input type='submit' id='submit' name='submit' value='Edit' /></td></tr>
</table>
</fieldset>
</form>
)
#----------------------------------------------------
# The main Ruby code is here.
#----------------------------------------------------
college = cgi['college']
dept = cgi['dept']
course = cgi['course']
desc = cgi['desc']
submit = cgi['submit']
if ((submit == 'Edit') && (formHasData([college, dept, course, desc])))
# Beware. The print function above includes CR by default.
# BUT the fp.write function below does not include the CR.
fp = File.new('../data/courses.tmp', 'w')
for i in 0..ncourses-1
if (i == id)
dataToWrite = sprintf("%s|%s|%s|%s\n", college,dept,course,desc)
# print (dataToWrite) # debug
fp.write (dataToWrite)
else
dataToWrite = sprintf("%s", courses[i])
# print (dataToWrite) # debug
fp.write (dataToWrite)
end
end
fp.close
File.rename("../data/courses.txt", "../data/courses.bak");
File.rename("../data/courses.tmp", "../data/courses.txt");
print "<p>The following data was edited in the data file: </p>\n";
print "<table class='results'>\n";
print "<tr><th>Field</th><th>Old data</th><th>New data</th></tr>\n"
printf( " <tr><td>College</td><td>%s</td><td>%s</td></tr>", coursedata[0], college)
printf( " <tr><td>Department</td><td>%s</td><td>%s</td></tr>", coursedata[1], dept)
printf( " <tr><td>Course #</td><td>%s</td><td>%s</td></tr>", coursedata[2], course)
printf( " <tr><td>Course name</td><td>%s</td><td>%s</td></tr>", coursedata[3], desc)
print "</table>"
end
#----------------------------------------------------
print %q(
<p><a href='fread.rb'>Show the contents of this file »</a></p>
<p><a href='source.rb?f=4'>Show Ruby source code »</a>
</div>
)
footers()