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.

merle
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(@user) do |form| %> <%= form.text_field(:name) %> <%= form.text_field(:login) %> <%= form.submit"Update User" %> <% end %> <form action="/site/users/2" method="post"> <input name="_method" type="hidden" value="put" /> <input id="user_login" name="user[login]“ size="30" type="text" value="pal" /> <input id="user_name" name="user[name]“ size="30" type="text" value="Philip Levis" /> <input name="commit" type="submit“ value="Update User" /> </form> Object representingform

  3. Customize Format <%= form_for(@user) do |form| %> <table cellspacing="0" class="user_form"> <tr><td> <div class="user_field"> <%= f.label "Login*" %><br /> <%= f.text_field :login %> </div> <div class="user_field"> <%= f.label "Name*" %><br /> <%= f.text_field :name %> </div> <div class="action"> <%= form.submit "Update User" %> </div> </td></tr> </table> <% end %> CS 142 Lecture Notes: Forms

  4. Post Action Method Hash with all of form data def update @user = User.find(params[:id]) @user.name = params[:user][:name] @user.login = params[:user][:login] if @user.save redirect_to@user, :notice => 'Success.' else render :action => :edit end end Redirects on success Redisplay form on error CS 142 Lecture Notes: Forms

  5. More Compact Method Security checks in cause update to fail def update @user = User.find(params[:id]) if @user.update(params[:user]) redirect_to@user, :notice => 'Success.' else render :action => :edit end end CS 142 Lecture Notes: Forms

  6. Rails 4 Pattern def update @user = User.find(params[:id]) if @user.update(user_params(params[:user])) redirect_to@user, :notice => 'Success. else render :action => :edit end end private def user_params(params) returnparams.permit(:name, :login) end Creates new hashcontaining onlyspecified elements CS 142 Lecture Notes: Forms

  7. Creating New Record defcreate @user = User.new(user_params(params[:user])) if @user.save() then redirect_to(@user, :notice => "Success.") else render(:action => :new) end end CS 142 Lecture Notes: Forms

  8. Validation (in Model) Built-in validator class User < ActiveRecord::Base validates :login, :presence => true, :uniqueness => true validates_format_of :login, :with => /[a-zA-Z0-9_-]+/, :message => "only letters, numbers, underscores (_), and dashes (-) are allowed" def validate_name if (name == "Kurt Cobain") then errors.add(:name, "Liar!") end end validate :validate_name end Custom validation method Saves error info CS 142 Lecture Notes: Forms

  9. Error Messages <%= form_for(@user) do |form| %> <% if @user.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2> <ul> <% @user.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <%= form.text_field(:name) %> <%= form.text_field(:login) %> <%= form.submit "Update User" %> <% end %> CS 142 Lecture Notes: Forms

  10. File Uploads with Rails <%= form_for(@student) do |f| %> ... <%= f.file_field:input_file%> ... <% end %> In form post method: params[:user][:input_file].read() params[:user][:input_file].original_filename CS 142 Lecture Notes: Forms

  11. CS 142 Lecture Notes: Cookies

More Related