120 likes | 208 Vues
Learn how to create, customize, and validate forms in Rails applications for optimal user interaction. Develop clean and secure form handling methods following object representation patterns. Enhance error signaling and user feedback with effective validation techniques.
E N D
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
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
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
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
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
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
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
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
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
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