Harbor Documentation

Harbor::ViewContext::Helpers::Form

Methods

Public Instance Methods

form(action, options = {}, &block)

Form helper which abstracts away setting necessary information:

  1. If no enctype is passed, and a file input is found, uses
     multipart/form-data.
  2. Creates a hidden input for setting _method when method option
     is put or delete.
      # File lib/harbor/view_context/helpers/form.rb, line 10
10:   def form(action, options = {}, &block)
11:     method = options.delete(:method) || :post
12: 
13:     get = method == :get
14:     post = method == :post
15: 
16:     body = capture(&block)
17: 
18:     enctype = options.delete(:enctype) || (body =~ /\<input[^>]+?type\=["']file["']/ ? "multipart/form-data" : nil)
19: 
20:     with_buffer(block) do |buffer|
21:       buffer << "<form action=\"#{action}\" method=\"#{get ? "get" : "post"}\""
22:       buffer << " enctype=\"#{enctype}\"" if enctype
23:       buffer << " #{options.map { |k, v| "#{k}=\"#{v}\"" }.join(" ")}" unless options.empty?
24:       buffer << ">\n"
25: 
26:       unless get || post
27:         buffer << "  <input type=\"hidden\" name=\"_method\" value=\"#{method}\">\n"
28:       end
29: 
30:       buffer << body
31:       buffer << "</form>\n"
32:     end
33:   end