Title: Simple Form
1Simple Form
ltform action"/x/y/z" method"POST"gt Value1
ltinput type"text" name"value1"/gtltbr /gt
Value2 ltinput type"text" name"value2"/gt
ltinput type"submit" value"Submit"/gt lt/formgt
2Rails Form Helpers
Name of modelclass (Student)
Name of variable containing data (_at_student)
lt form_for(student, url gt action gt
modify, id gt_at_student.id) do form gt
lttable class"form"gt lttrgt lttd
class"label"gtNamelttdgt lttdgtlt
form.text_field(name) gtlttdgt lt/trgt lttrgt
lttd class"label"gtDate of birthlttdgt
lttdgtlt form.text_field(birth) gtlttdgt lt/trgt
... lttablegt lt submit_tag "Modify
Student" gt lt end gt
Initial value will be_at_student.name
Text to displayin submit button
3Post Action Method
Hash with all of form data
def modify _at_student Student.find(paramsid)
if _at_student.update_attributes(paramsstudent)
then redirect_to(action gt show) else
render(action gt edit) end end
Redirects on success
4Validation
Custom validation method
class Student lt ActiveRecordBase def
validate if (gpa lt 0) (gpa gt 4.0) then
errors.add(gpa, "must be between 0.0 and
4.0") end end validates_format_of
birth, with gt /\d\d\d\d-\d\d-\d\d/,
message gt "must have format YYYY-MM-DD end
Built-in validator
Saves error info
5Error Message Helper
lt error_messages_for(student)gt lt
form_for(student, url gt action gt modify,
id gt_at_student.id) do form gt ... lt end gt
6(No Transcript)