simple form
Download
Skip this Video
Download Presentation
Simple Form

Loading in 2 Seconds...

play fullscreen
1 / 8

Simple Form - PowerPoint PPT Presentation


  • 64 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Simple Form' - nico


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
simple form
Simple Form

Product:

Price:

CS 142 Lecture Notes: Forms

rails form helpers
Rails Form Helpers

Describes type, provides initial values

<%= form_for(@student, :url => {:action => :modify,

:id => @student.id}) do |form| %>

<%= form.text_field(:name) %>

<%= form.text_field(:birth) %>

<%= form.submit"Modify Student" %>

<% end %>

Object representingform

CS 142 Lecture Notes: Forms

customize format
Customize Format

<%= form_for(@student, :url => {:action => :modify,

:id =>@student.id}) do |form| %>

...

<%= form.label(:name, "Name:")%><%= form.text_field(:name) %>
<%= form.label(:birth, "Date of birth:")%><%=form.text_field(:birth) %>

<%= form.submit"Modify Student" %>

<% end %>

CS 142 Lecture Notes: Forms

post action method
Post Action Method

Hash with all of form data

def modify

@student = Student.find(params[:id])

if @student.update_attributes(params[:student]) then

redirect_to(:action => :show)

else

render(:action => :edit)

end

end

Redirects on success

Redisplay form on error

CS 142 Lecture Notes: Forms

validation in model
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

error messages
Error Messages

<% @student.errors.full_messages.each do |msg| %>

<%= msg %>

<% end %>

form_for(@student, :url => {:action => :modify,

:id =>@student.id}) do |form| %>

...

<%= form.label(:birth, "Date of birth:")%>

<%= form.text_field(:birth) %>

...

<% end %>

CS 142 Lecture Notes: Forms

file uploads with rails
File Uploads with Rails

<% form_for(:student, :html=>{:multipart => true} :url => {...}) 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

ad