Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

CGI::HtmlExtension#form

form(method = "post", action = script_name, enctype = "application/x-www-form-urlencoded") { || ... }

Generate a Form element as a string.

method should be either “get” or “post”, and defaults to the latter. action defaults to the current CGI script name. enctype defaults to “application/x-www-form-urlencoded”.

Alternatively, the attributes can be specified as a hash.

See also multipart_form() for forms that include file uploads.

Example
form{ "string" }
  # <FORM METHOD="post" ENCTYPE="application/x-www-form-urlencoded">string</FORM>

form("get") { "string" }
  # <FORM METHOD="get" ENCTYPE="application/x-www-form-urlencoded">string</FORM>

form("get", "url") { "string" }
  # <FORM METHOD="get" ACTION="url" ENCTYPE="application/x-www-form-urlencoded">string</FORM>

form("METHOD" => "post", "ENCTYPE" => "enctype") { "string" }
  # <FORM METHOD="post" ENCTYPE="enctype">string</FORM>
See also #file_field #hidden #checkbox_group #html also in 3.4 · 3.3 · 3.1 · 3.0