1 / 11

Simple Form

Simple Form. <form action ="/product/update" method="post"> Product: <input type="text" name = " product"/> < br /> Price: <input type="text" name ="price " value= " 49.95"/> < br /> < input type="submit" value="Submit"/> </ form >. Rails Form Helpers.

archie
Download Presentation

Simple Form

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Form <form action="/product/update" method="post"> Product: <input type="text" name="product"/><br /> Price: <input type="text" name="price" value="49.95"/><br /> <input type="submit" value="Submit"/> </form> CS 142 Lecture Notes: Forms

  2. Rails Form Helpers Describes type, provides initial values <%= form_for(@student, method: :post, url: {action: :update, id: @student.id}) do |form| %> <%= form.text_field(:name) %> <%= 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> Object representingform

  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> <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

  4. Post Action Method Hash with all of form data defupdate @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.savethen redirect_to(:action => :show) else render(:action => :edit) end end Redirects on success Redisplay form on error CS 142 Lecture Notes: Forms

  5. More Compact Approach Security checks in Rails 4 cause update to fail defupdate @student = Student.find(params[:id]) if @student.update(params[:student]) then redirect_to(:action => :show) else render(:action => :edit) end end CS 142 Lecture Notes: Forms

  6. Rails 4 Pattern defupdate @student = Student.find(params[:id]) if @student.update(student_params( params[:student])) then redirect_to(:action => :show) else render(:action => :edit) end end private defstudent_params(params) return params.permit(:name, :birth, :gpa, :grad) end Creates new hashcontaining onlyspecified elements CS 142 Lecture Notes: Forms

  7. Creating New Record defcreate @student = Student.new(student_params( params[:student]) if @student.save() then redirect_to(:action => :show) else render(:action => :edit) end end CS 142 Lecture Notes: Forms

  8. Validation (in Model) Built-in validator class Student < ActiveRecord::Basevalidates_format_of:birth, :with => /\d\d\d\d-\d\d-\d\d/, :message => "must have format YYYY-MM-DD" defvalidate_gpa if (gpa < 0) || (gpa > 4.0) then errors.add(:gpa, "must be between 0.0 and 4.0") end end validate :validate_gpa end Custom validation method Saves error info CS 142 Lecture Notes: Forms

  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

  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

  11. CS 142 Lecture Notes: Cookies

More Related