Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 142 Lecture Notes: FormsSlide 1 Simple Form Product: Price:

Similar presentations


Presentation on theme: "CS 142 Lecture Notes: FormsSlide 1 Simple Form Product: Price:"— Presentation transcript:

1 CS 142 Lecture Notes: FormsSlide 1 Simple Form Product: Price:

2 CS 142 Lecture Notes: FormsSlide 2 Rails Form Helpers {:action => :modify, :id do |form| %> Describes type, provides initial values Object representing form

3 CS 142 Lecture Notes: FormsSlide 3 Customize Format {:action => :modify, :id do |form| %>...

4 CS 142 Lecture Notes: FormsSlide 4 Post Action Method def = Student.find(params[:id]) then redirect_to(:action => :show) else render(:action => :edit) end Hash with all of form data Redirects on success Redisplay form on error

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

6 CS 142 Lecture Notes: FormsSlide 6 Error Messages :url => {:action => :modify, :id do |form| %>......

7 CS 142 Lecture Notes: FormsSlide 7 File Uploads with Rails {:multipart => true} :url => {...}) do |form| %> In form post method: params[:student][:photo].read() params[:student][:photo].original_filename

8 CS 142 Lecture Notes: CookiesSlide 8


Download ppt "CS 142 Lecture Notes: FormsSlide 1 Simple Form Product: Price:"

Similar presentations


Ads by Google