Simple Form form actionproductupdate methodpost Product input typetext
- Slides: 12
Simple Form <form action="/product/update" method="post"> Product: <input type="text" name="product"/> Price: <input type="text" name="price" value="49. 95"/> <input type="submit" value="Submit"/> </form> CS 142 Lecture Notes: Forms Slide 1
Controller Code for Form class Students. Controller < Application. Controller def index. . . end def edit if params[: id] then @student = Student. find(params[: id]) else @student = Student. new() end def update. . . end Display form Handle post Slide 2
Rails Form Helpers Describes type, provides initial values <%= form_for(@student, method: : post, url: {action: : update, id: @student. id}) do |form| %> Object representing <%= form. text_field(: name) %> form <%= form. text_field(: birth) %> <%= form. submit "Modify Student" %> <% end %> <form action="/student/update/4" method="post"> <input id="student_name" name="student[name]“ size="30" type="text" value="Chen" /> <input id="student_birth" name="student[birth]“ size="30" type="text" value="1990 -02 -04" /> <input name="commit" type="submit“ value="Modify Student" /> </form> Slide 3
Customize Format <%= form_for(@student, method: : post, url: {action: : update, id: @student. id}) do |form| %> <table class="form"> <tr> <td><%= form. label(: name, "Name: ")%></td> <td><%= form. text_field(: name) %></td> </tr> <td><%= form. label(: birth, "Date of birth: ")%></td> <td><%= form. text_field(: birth) %></td> </tr>. . . <table> <%= form. submit "Modify Student" %> <% end %> CS 142 Lecture Notes: Forms Slide 4
Post Action Method Hash with all of form data def update @student = Student. find(params[: id]) ]) @student. name = params[: student][: name] @student. birth = params[: student][: birth] @student. gpa = params[: student][: gpa] @student. grad = params[: student][: grad] if @student. save then redirect_to(: action => : index) else render(: action => : edit) end Redirects on success Redisplay form on error CS 142 Lecture Notes: Forms Slide 5
More Compact Approach def update @student = Student. find(params[: id]) if @student. update(params[: student]) then redirect_to(: action => : index) else render(: action => : edit) end Security checks in Rails 4 cause update to fail CS 142 Lecture Notes: Forms Slide 6
Rails 4 Pattern def update @student = Student. find(params[: id]) if @student. update(student_params( params[: student])) then redirect_to(: action => : index) else Creates new object render(: action => : edit) where specified end elements allowed for end mass update private def student_params(params) return params. permit(: name, : birth, : gpa, : grad) end CS 142 Lecture Notes: Forms Slide 7
Creating New Record def create @student = Student. new(student_params( params[: student]) if @student. save() then redirect_to(: action => : index) else render(: action => : edit) end CS 142 Lecture Notes: Forms Slide 8
Validation (in Model) Built-in validator class Student < Active. Record: : Base validates : birth, format: { with: /dd-dd/, message: "must have format YYYY-MM-DD" } Custom validation method def validate_gpa if (gpa < 0) || (gpa > 4. 0) then errors. add(: gpa, "must be between 0. 0 and 4. 0") end Saves error info validate : validate_gpa end CS 142 Lecture Notes: Forms Slide 9
Error Messages <% @student. errors. full_messages. each do |msg| %> <p><%= msg %></p> <% end %> <%= form_for(@student, method: : post, url: {action => : update, id: @student. id}) do |form| %>. . . <%= form. label(: birth, "Date of birth: ")%> <%= form. text_field(: birth) %>. . . <% end %> CS 142 Lecture Notes: Forms Slide 10
File Uploads with Rails <%= form_for(: student, method: . . . ) do |form| %>. . . <%= form. file_field(: photo) %>. . . <% end %> In form post method: params[: student][: photo]. read() params[: student][: photo]. original_filename CS 142 Lecture Notes: Forms Slide 11
CS 142 Lecture Notes: Cookies Slide 12
- Simple form input value
- Peripheral output adalah.
- Roughly-tuned input definition
- Present simple present continuous past simple future simple
- Simple past simple present simple future
- Past continuous present simple
- Past simple future simple present simple
- Simple present, simple past, simple future
- Future simple present continuous
- Simple present past future tense words
- Frases afirmativas simple present
- Present simple past simple future simple
- Product life cycle kotler