Simple Form form actionxyz methodPOST Value 1 input

  • Slides: 6
Download presentation
Simple Form <form action="/x/y/z" method="POST"> Value 1: <input type="text" name="value 1"/> Value 2: <input

Simple Form <form action="/x/y/z" method="POST"> Value 1: <input type="text" name="value 1"/> Value 2: <input type="text" name="value 2"/> <input type="submit" value="Submit"/> </form> CS 142 Lecture Notes: Forms 1

Name of model class (Student) Rails Form Helpers Name of variable containing data (@student)

Name of model class (Student) Rails Form Helpers Name of variable containing data (@student) <% form_for(: student, : url => {: action => : modify, : id =>@student. id}) do |form| %> <table class="form"> Initial value will be <tr> @student. name <td class="label">Name: <td><%= form. text_field(: name) %><td> </tr> <td class="label">Date of birth: <td><%= form. text_field(: birth) %><td> </tr> Text to display. . . in submit button <table> <%= submit_tag "Modify Student" %> <% end %> CS 142 Lecture Notes: Forms 2

Post Action Method Hash with all of form data def modify @student = Student.

Post Action Method Hash with all of form data def modify @student = Student. find(params[: id]) if @student. update_attributes(params[: student]) then redirect_to(: action => : show) else render(: action => : edit) end Redirects on success CS 142 Lecture Notes: Forms 3

Validation Custom validation method class Student < Active. Record: : Base def validate if

Validation Custom validation method class Student < Active. Record: : Base def validate if (gpa < 0) || (gpa > 4. 0) then errors. add(: gpa, "must be between 0. 0 and 4. 0") end Built-in validator Saves error info validates_format_of : birth, : with => /dd-dd/, : message => "must have format YYYY-MM-DD“ end CS 142 Lecture Notes: Forms 4

Error Message Helper <%= error_messages_for(: student)%> <% form_for(: student, : url => {: action

Error Message Helper <%= error_messages_for(: student)%> <% form_for(: student, : url => {: action => : modify, : id =>@student. id}) do |form| %>. . . <% end %> CS 142 Lecture Notes: Forms 5

CS 142 Lecture Notes: Forms 6

CS 142 Lecture Notes: Forms 6