Download presentation
Presentation is loading. Please wait.
1
CS 142 Lecture Notes: FormsSlide 1 Simple Form Value1: Value2:
2
CS 142 Lecture Notes: FormsSlide 2 Rails Form Helpers {:action => :modify, :id =>@student.id}) do |form| %> Name: Date of birth:... Name of model class (Student) Name of variable containing data (@student) Initial value will be @student.name Text to display in submit button <input id="student_name" name="student[name]" size="30" type="text" value="Hernandez" />
3
CS 142 Lecture Notes: FormsSlide 3 Post Action Method def modify @student = Student.find(params[:id]) if @student.update_attributes(params[:student]) then redirect_to(:action => :show) else render(:action => :edit) end Hash with all of form data Redirects on success
4
CS 142 Lecture Notes: FormsSlide 4 Validation class Student < ActiveRecord::Base def validate if (gpa 4.0) then errors.add(:gpa, "must be between 0.0 and 4.0") end validates_format_of :birth, :with => /\d\d\d\d-\d\d-\d\d/, :message => "must have format YYYY-MM-DD“ end Custom validation method Built-in validator Saves error info
5
CS 142 Lecture Notes: FormsSlide 5 Error Message Helper {:action => :modify, :id =>@student.id}) do |form| %>......
6
CS 142 Lecture Notes: FormsSlide 6 File Uploads with Rails {:multipart => true} :url => {...}) do |form| %>...... In form post method: params[:student][:photo].read() params[:student][:photo].original_filename
7
CS 142 Lecture Notes: FormsSlide 7
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.