© 2019 Martin J.
Dürst 青山学院大学
ミニテスト
- デスクトップPC (強く推薦) 又はノートPCで https://moo.sw.it.aoyama.ac.jp/
にログイン済み
- ナビゲーションは左に畳み、ブラウザは全画面に拡大
- もう片方のマシーンで電源を切る
(ノートPCの場合、鞄に)
- 授業開始まで教科書、資料、筆箱、財布などを鞄に入れ、鞄は床に
- テスト終了後その場で待つ
Today's Schedule
- Minitest
- Last week's exercises and homework
- Simplifying HTML output: Templates
- Input to Web applications: Forms and URI query parts
- Today's exercises
3年生対象大学院進学説明会
- 5月15日 (水) 13:20-14:50 (3限)
- E-302
- 全体概要説明と学科別・個別相談
Last Lecture's Exercises
(see handouts)
Last Lecture's Homework
- Install rails with the following command in "Start Command Prompt with
Ruby"
gem install rails -v 5.2.3
- Check the installed version with
rails --version
- Use
irb
to play with arrays and iterators
(each
, map
, select
,
reject
, inject
,…).
Course Overview
- 1st week: HTML markup ⇒ structure
- 2nd week: CSS stylesheet ⇒ styling
- 3rd week: Programming with Ruby
- 4th week: Dynamically create a Web page with a program, and serve it from
a Web server
- Today: Simplify Web page creation with templates, make them more flexible
with forms
Some Problems of CGI
- Each page needs a separate program
- Tendency to put too many things into a single page
- External program execution is inefficient
- Many output statements
Some Problems of CGI
- Each page needs a separate program
- Tendency to put too many things into a single page
- External program execution is inefficient
- Many output statements
Templates
- Basic idea: Replace Web page output in program code
with code islands in literal Web page
- Available for many programming languages:
JSP (Java Server pages), ASP (Microsoft Active Server Pages)
- Programming language that started as a template engine: PHP
- Templates for Ruby: ERB (embedded Ruby)
ERB Template Syntax
- File extension for Web pages is
.html.erb
- Full Web page from
<!DOCTYPE
to
</html>
- Embedded program code:
Comparing CGI and ERB
|
CGI |
ERB |
Web page contents |
explicit output |
direct |
Web page markup |
explitic output |
direct |
Program code |
direct |
<% ... %> |
Program results |
direct calculation, explicit output |
<%= ... %> |
Web Forms
- Input from users to Web applications mostly via Web forms
- Main form element in HTML:
form
- Start tag syntax:
<form method='get'>
- Two methods (
get
and post
), today we use
get
- Request to server by an URI with query part, e.g.:
http://localhost:12000/my_template.html.erb?exponent=0.7&step=3
URIs, URLs, and IRIs
- URI (Universal/Uniform Resource Identifier): Formally correct
name (RFC 3986)
- URL (Universal/Uniform Resource Locator): Widely used term (URL 'standard')
- IRI (Internationalized Resource Identifier): Extension allowing
non-ASCII characters (RFC
3987)
- Caution: アイデンティファイア, NOT アイデンティファ―
- Start with scheme, e.g.
http:
, https:
,
ftp:
, mailto:
, tel:
,
data:
, …
- Part after
?
is query part
Query Part Syntax
- Each form field (e.g.
input
element) produces a name-value
pair, separated by =
- Fields (parameters) are separated by
&
- Example:
http://my.site.com/page.html?name1=value1&name2=value2
- Server-side libraries and frameworks are designed to handle query parts
easily
Example: servlet_request.query[name1]
⇒ value2
- Query values are strings, so conversion is necessary
- Query parts may contain arbitrary values, so be careful about security
(see Bobby Tables)
Form Fields
- There are many form fields, but the most important is
input
,
in particular <input type='text'>
for textual input.
- Form fields should be labeled with
label
elements
- Initial values can be given with a
value
attribute
(for <input type='submit'>
, the value
is
the text on the button)
- The
name
attribute gives the parameter name
Ruby Iterators: Loops with Steps
There are many ways to loop with steps.
Example: Loop from 0 to 20 with steps of 2:
(0..20).each { |n| ... if n%2 == 0 }
(0..20).select { |n| n%2 == 0 }.each { ... }
(0..10).each { |n| ... n*2 }
(0..10).map { |n| n*2 }.each { |n| ... }
(0..20).each_slice { |a| ... a[0] }
(0..20).step(2) { |n| ... }
0.step(20, 2) { |n| ... }
Make sure you know at least one of these.
Exercise 5a: A Template to Convert Marks
- Convert your program from exercise 4c to an ERB template in a file
5a_convert_marks.html.erb
.
- Enclose Ruby code (except HTML output) with
<%
and
%>
or <%=
and %>
.
- Remove puts/print statements (replace with
<%=...
where necessary).
- Remove the HTTP header/empty line output.
- Replace
ENV['QUERY_STRING']
with
servlet_request...
- Before or after the definition of the
convert_mark
function,
add a line saying:
extend self
- Start
webrick
and test your program with various
exponents.
- Save the source (HTML only) of your Web page and validate it. Fix any
validation errors.
- Submit the program
5a_convert_marks.html.erb
to Moodle
(deadline: 17:20).
Exercise 5b: A Form to Convert Marks
- Add a
form
to your template from exercise 5a in a file
5b_convert_marks_form.html.erb
.
- Hint: You can use the "Exponentiation in Ruby" example for hints.
- The form uses two inputs:
exponent
and step
.
A step
of 5 will only output converted marks for 0, 5, 10, and
so on.
- Use a default of 1 if the
step
is not given, and 0.5 for the
exponent
.
- Make sure the form values are set to the input values when repeatedly
using the form.
- Start
webrick
(if not already started) and test your program
with various exponents and steps,
and with URIs missing some or all of the parameters.
- Save the source (HTML only) of your Web page and validate it. Fix any
validation errors.
- Submit the program
5b_convert_marks_form.html.erb
to Moodle
(deadline: 18:20).
Exercise 5c: A Table of Students
- Download the student data from Moodle
- Create a template in a file
5c_student_table.html.erb
that
outputs this student data in a table, including the grade average
- Use a parameter (
sort=familyname
,
sort=givenname
, sort=english
,
sort=math
, sort=average
) to indicate sort
order.
- Use the Ruby
sort
or sort_by
method to sort the
array before starting with the ouput
(hint: check out these methods on the Web)
- As usual, check your template using webrick and validate it
- Submit the program
5c_student_table.html.erb
to Moodle
(deadline: May 15 (Wednesday), 19:00)
Homework: Sqlite Installation