/
CS 142 Lecture Notes: Forms CS 142 Lecture Notes: Forms

CS 142 Lecture Notes: Forms - PowerPoint Presentation

alida-meadow
alida-meadow . @alida-meadow
Follow
396 views
Uploaded On 2016-06-30

CS 142 Lecture Notes: Forms - PPT Presentation

Slide 1 Simple Form ltform action productupdate methodpostgt Product ltinput typetext name productgt lt br gt Price ltinput typetext name ID: 383347

form student action params student form params action update birth slide 142 notes lecture forms method text post def

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CS 142 Lecture Notes: Forms" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

CS 142 Lecture Notes: Forms

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

Slide

2Controller Code for Form

class

StudentsController < ApplicationController def index ... end def edit if params[:id] then @student = Student.find(params[:id]) else @student = Student.new() end end def update ... endend

Display

form

Handle

postSlide3

Slide

3Rails Form Helpers

<%=

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>

Describes type, provides initial values

Object representingformSlide4

CS 142 Lecture Notes: Forms

Slide

4

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

CS 142 Lecture Notes: Forms

Slide 5

Post Action Method

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

Hash with all of form data

Redirects on success

Redisplay form on errorSlide6

Security checks in Rails 4 cause update to fail

CS 142 Lecture Notes: Forms

Slide

6More Compact Approachdef update @student = Student.find(params[:id]) if @student.update(params[:student]) then redirect_to(:action => :index) else render(:action => :edit) endendSlide7

CS 142 Lecture Notes: Forms

Slide 7

Rails 4 Pattern

def update @student = Student.find(params[:id]) if @student.update(student_params( params[:student])) then redirect_to(:action => :index) else render(:action => :edit) endendprivatedef

student_params

(

params

) return params.permit(:name, :birth, :gpa, :grad)end

Creates new

object

where specified

elements allowed for

mass updateSlide8

CS 142 Lecture Notes: Forms

Slide 8

Creating New Record

def create @student = Student.new(student_params( params[:student]) if @student.save() then redirect_to(:action => :index) else render(:action => :edit) endendSlide9

CS 142 Lecture Notes: Forms

Slide 9

Validation (in Model)

class Student < ActiveRecord::Base validates :birth, format: { with: /\d\d\d\d-\d\d-\d\d/, message: "must have format YYYY-MM-DD" } def validate_gpa if (gpa

< 0) || (

gpa

> 4.0)

then

errors.add(:gpa, "must be between 0.0 and 4.0") end end validate :validate_gpaend

Custom validation method

Built-in validator

Saves error infoSlide10

CS 142 Lecture Notes: Forms

Slide 10

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

CS 142 Lecture Notes: Forms

Slide 11

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_filenameSlide12

CS 142 Lecture Notes: CookiesSlide 12