Skip to content

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

instance method

CGI::HtmlExtension#text_field

text_field(name = "", value = nil, size = 40, maxlength = nil)

Generate a text field Input element, as a String.

name is the name of the input field. value is its initial value. size is the size of the input area. maxlength is the maximum length of input accepted.

Alternatively, the attributes can be specified as a hash.

Example
text_field("name")
  # <INPUT TYPE="text" NAME="name" SIZE="40">

text_field("name", "value")
  # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="40">

text_field("name", "value", 80)
  # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80">

text_field("name", "value", 80, 200)
  # <INPUT TYPE="text" NAME="name" VALUE="value" SIZE="80" MAXLENGTH="200">

text_field("NAME" => "name", "VALUE" => "value")
  # <INPUT TYPE="text" NAME="name" VALUE="value">
See also #textarea #submit also in 3.4 · 3.3 · 3.2 · 3.1