跳到內容 跳到搜尋

Action 視圖表單標籤幫手

提供許多用於建立不依賴於指定給範本的 Active Record 物件的表單標籤的方法,例如 FormHelper 所做的。您手動提供名稱和值。

注意:HTML 選項「disabled」、「readonly」和「multiple」都可以視為布林值。因此,指定「disabled: true」會給出「disabled="disabled"」。

方法
B
C
D
E
F
H
I
L
M
N
P
R
S
T
U
W
包含的模組

執行個體公開方法

button_tag(content_or_options = nil, options = nil, &block)

建立一個定義「submit」按鈕、「reset」按鈕或泛用按鈕的按鈕元素,例如可用於 JavaScript 中。您可以將按鈕標籤當做一般提交標籤使用,但它不支援於舊版瀏覽器中。不過,按鈕標籤允許更豐富的標籤,例如圖像和重點,因此,這個 helper 也會接受區塊。如果未給定類型,它會預設建立類型為「submit」的按鈕標籤。

選項

  • :data - 此選項可用於新增自訂資料屬性。

  • :disabled - 如果是真的,使用者就無法使用此輸入。

  • 任何其他金鑰會為標籤建立標準 HTML 選項。

範例

button_tag
# => <button name="button" type="submit">Button</button>

button_tag 'Reset', type: 'reset'
# => <button name="button" type="reset">Reset</button>

button_tag 'Button', type: 'button'
# => <button name="button" type="button">Button</button>

button_tag 'Reset', type: 'reset', disabled: true
# => <button name="button" type="reset" disabled="disabled">Reset</button>

button_tag(type: 'button') do
  content_tag(:strong, 'Ask me!')
end
# => <button name="button" type="button">
#     <strong>Ask me!</strong>
#    </button>
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 568
def button_tag(content_or_options = nil, options = nil, &block)
  if content_or_options.is_a? Hash
    options = content_or_options
  else
    options ||= {}
  end

  options = { "name" => "button", "type" => "submit" }.merge!(options.stringify_keys)

  if block_given?
    content_tag :button, options, &block
  else
    content_tag :button, content_or_options || "Button", options
  end
end

check_box_tag(name, *args)

別名,用於: checkbox_tag

checkbox_tag(name, options = {})
checkbox_tag(name, value, options = {})
checkbox_tag(name, value, checked, options = {})

建立核取方塊表單輸入標籤。

選項

  • :value - 輸入的值。預設為 "1"

  • :checked - 若設為 true,checkbox 將會預設勾選。

  • :disabled - 若設為 true,使用者將無法使用此輸入。

  • 任何其他金鑰會為標籤建立標準 HTML 選項。

範例

checkbox_tag 'accept'
# => <input id="accept" name="accept" type="checkbox" value="1" />

checkbox_tag 'rock', 'rock music'
# => <input id="rock" name="rock" type="checkbox" value="rock music" />

checkbox_tag 'receive_email', 'yes', true
# => <input checked="checked" id="receive_email" name="receive_email" type="checkbox" value="yes" />

checkbox_tag 'tos', 'yes', false, class: 'accept_tos'
# => <input class="accept_tos" id="tos" name="tos" type="checkbox" value="yes" />

checkbox_tag 'eula', 'accepted', false, disabled: true
# => <input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />
別名為: check_box_tag
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 456
def checkbox_tag(name, *args)
  if args.length >= 4
    raise ArgumentError, "wrong number of arguments (given #{args.length + 1}, expected 1..4)"
  end
  options = args.extract_options!
  value, checked = args.empty? ? ["1", false] : [*args, false]
  html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
  html_options["checked"] = "checked" if checked
  tag :input, html_options
end

color_field_tag(name, value = nil, options = {})

建立一個資料類型為「color」的文字欄位。

選項

支援與 text_field_tag 相同的選項。

範例

color_field_tag 'name'
# => <input id="name" name="name" type="color" />

color_field_tag 'color', '#DEF726'
# => <input id="color" name="color" type="color" value="#DEF726" />

color_field_tag 'color', nil, class: 'special_input'
# => <input class="special_input" id="color" name="color" type="color" />

color_field_tag 'color', '#DEF726', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="color" name="color" type="color" value="#DEF726" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 668
def color_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :color))
end

date_field_tag(name, value = nil, options = {})

建立一個資料類型為「date」的文字欄位。

選項

支援與 text_field_tag 相同的選項。

範例

date_field_tag 'name'
# => <input id="name" name="name" type="date" />

date_field_tag 'date', '2014-12-31'
# => <input id="date" name="date" type="date" value="2014-12-31" />

date_field_tag 'date', nil, class: 'special_input'
# => <input class="special_input" id="date" name="date" type="date" />

date_field_tag 'date', '2014-12-31', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="date" name="date" type="date" value="2014-12-31" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 738
def date_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :date))
end

datetime_field_tag(name, value = nil, options = {})

建立一個資料類型為「datetime-local」的文字欄位。

選項

支援與 text_field_tag 相同的選項。此外,還支援

  • :min - 可接受的最小值。

  • :max - 可接受的最大值。

  • :step - 可接受值的粒度。

  • :include_seconds - 在輸出時間戳記格式中包含秒(預設為 true)。

範例

datetime_field_tag 'name'
# => <input id="name" name="name" type="datetime-local" />

datetime_field_tag 'datetime', '2014-01-01T01:01'
# => <input id="datetime" name="datetime" type="datetime-local" value="2014-01-01T01:01" />

datetime_field_tag 'datetime', nil, class: 'special_input'
# => <input class="special_input" id="datetime" name="datetime" type="datetime-local" />

datetime_field_tag 'datetime', '2014-01-01T01:01', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="datetime" name="datetime" type="datetime-local" value="2014-01-01T01:01" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 797
def datetime_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: "datetime-local"))
end

datetime_local_field_tag(name, value = nil, options = {})

別名為: datetime_field_tag

email_field_tag(name, value = nil, options = {})

建立一個資料類型為「email」的文字欄位。

選項

支援與 text_field_tag 相同的選項。

範例

email_field_tag 'name'
# => <input id="name" name="name" type="email" />

email_field_tag 'email', 'email@example.com'
# => <input id="email" name="email" type="email" value="email@example.com" />

email_field_tag 'email', nil, class: 'special_input'
# => <input class="special_input" id="email" name="email" type="email" />

email_field_tag 'email', 'email@example.com', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="email" name="email" type="email" value="email@example.com" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 899
def email_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :email))
end

field_id(object_name, method_name, *suffixes, index: nil, namespace: nil)

產生一個給定名稱和欄位組合的 HTML id 屬性值

傳回 FormBuilder 為給定屬性名稱產生並的值。

<%= label_tag :post, :title %>
<%= text_field :post, :title, aria: { describedby: field_id(:post, :title, :error) } %>
<%= tag.span("is blank", id: field_id(:post, :title, :error) %>

在上面的範例中,由 text_field 呼叫所建立的 <input type="text"> 元素宣告了一個 aria-describedby 屬性,參考 <span> 元素,共用一個 id 來源(在本例中為 post_title)。

# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 102
def field_id(object_name, method_name, *suffixes, index: nil, namespace: nil)
  if object_name.respond_to?(:model_name)
    object_name = object_name.model_name.singular
  end

  sanitized_object_name = object_name.to_s.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").delete_suffix("_")

  sanitized_method_name = method_name.to_s.delete_suffix("?")

  [
    namespace,
    sanitized_object_name.presence,
    (index unless sanitized_object_name.empty?),
    sanitized_method_name,
    *suffixes,
  ].tap(&:compact!).join("_")
end

field_name(object_name, method_name, *method_names, multiple: false, index: nil)

根據給定的名稱和欄位組合,產生 HTML name 屬性值

傳回 FormBuilder 為給定屬性名稱產生並的值。

<%= text_field :post, :title, name: field_name(:post, :title, :subtitle) %>
<%# => <input type="text" name="post[title][subtitle]"> %>

<%= text_field :post, :tag, name: field_name(:post, :tag, multiple: true) %>
<%# => <input type="text" name="post[tag][]"> %>
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 132
def field_name(object_name, method_name, *method_names, multiple: false, index: nil)
  names = method_names.map! { |name| "[#{name}]" }.join

  # a little duplication to construct fewer strings
  case
  when object_name.blank?
    "#{method_name}#{names}#{multiple ? "[]" : ""}"
  when index
    "#{object_name}[#{index}][#{method_name}]#{names}#{multiple ? "[]" : ""}"
  else
    "#{object_name}[#{method_name}]#{names}#{multiple ? "[]" : ""}"
  end
end

field_set_tag(legend = nil, options = nil, &block)

建立欄位組,用於將 HTML 表單元素分組。

legend 將成為欄位組的標題(W3C 視為選用)。options 接受與標籤相同的數值。

範例

<%= field_set_tag do %>
  <p><%= text_field_tag 'name' %></p>
<% end %>
# => <fieldset><p><input id="name" name="name" type="text" /></p></fieldset>

<%= field_set_tag 'Your details' do %>
  <p><%= text_field_tag 'name' %></p>
<% end %>
# => <fieldset><legend>Your details</legend><p><input id="name" name="name" type="text" /></p></fieldset>

<%= field_set_tag nil, class: 'format' do %>
  <p><%= text_field_tag 'name' %></p>
<% end %>
# => <fieldset class="format"><p><input id="name" name="name" type="text" /></p></fieldset>
也別名為:fieldset_tag
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 640
def field_set_tag(legend = nil, options = nil, &block)
  content = []
  content << content_tag("legend", legend) unless legend.blank?
  content << capture(&block) if block_given?

  content_tag(:fieldset, safe_join(content), options)
end

fieldset_tag(legend = nil, options = nil, &block)

別名為:field_set_tag

file_field_tag(name, options = {})

建立檔案上傳欄位。如果您使用檔案上傳,也需要為表單標籤設定 multipart 選項

<%= form_tag '/upload', multipart: true do %>
  <label for="file">File to Upload</label> <%= file_field_tag "file" %>
  <%= submit_tag %>
<% end %>

指定的網址會傳遞包含所選檔案的 File 物件,或如果欄位留空,則為 StringIO 物件。

選項

  • 建立標籤的標準 HTML 屬性。

  • :disabled - 若設為 true,使用者將無法使用此輸入。

  • :multiple - 如果設定為 true,*在大部分更新的瀏覽器中*,使用者會被允許選取多個檔案。

  • :accept - 如果設定為一個或多個 MIME 類型,使用者在選取檔案時會看到建議的篩選器。您仍需要設定模式驗證。

範例

file_field_tag 'attachment'
# => <input id="attachment" name="attachment" type="file" />

file_field_tag 'avatar', class: 'profile_input'
# => <input class="profile_input" id="avatar" name="avatar" type="file" />

file_field_tag 'picture', disabled: true
# => <input disabled="disabled" id="picture" name="picture" type="file" />

file_field_tag 'resume', value: '~/resume.doc'
# => <input id="resume" name="resume" type="file" value="~/resume.doc" />

file_field_tag 'user_pic', accept: 'image/png,image/gif,image/jpeg'
# => <input accept="image/png,image/gif,image/jpeg" id="user_pic" name="user_pic" type="file" />

file_field_tag 'file', accept: 'text/html', class: 'upload', value: 'index.html'
# => <input accept="text/html" class="upload" id="file" name="file" type="file" value="index.html" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 347
def file_field_tag(name, options = {})
  text_field_tag(name, nil, convert_direct_upload_option_to_url(options.merge(type: :file)))
end

form_tag(url_for_options = {}, options = {}, &block)

啟動表單標籤,將動作設定為透過 url_for_options 組態的網址,就像 ActionController::Base#url_for 一樣。表單的預設方法為 POST。

選項

  • :multipart - 如果設定為 true,enctype 會設定為「multipart/form-data」。

  • :method - 送出表單時要使用的方法,通常是「get」或「post」。如果使用「patch」、「put」、「delete」或其他動詞,則會加上一個名稱為 _method 的隱藏輸入,以模擬 POST 上的動詞。

  • :authenticity_token - 要在表單中使用的真實性識別碼。僅在需要傳遞自訂的真實性識別碼字串時使用,或完全不新增真實性識別碼欄位(傳遞 false)。遠端表單可以透過設定 config.action_view.embed_authenticity_token_in_remote_forms = false 省略內嵌真實性識別碼。當您對表單進行區塊快取時,這會很有用。遠端表單會從 meta 標籤取得真實性識別碼,因此除非您支援沒有 JavaScript 的瀏覽器,否則不需要內嵌。

  • :remote - 如果設定為 true,將允許 Unobtrusive JavaScript 驅動程式控制送出行為。此行為預設為 ajax 送出。

  • :enforce_utf8 - 如果設定為 false,不會產生名稱為 utf8 的隱藏輸入。

  • 任何其他金鑰都會建立標籤的標準 HTML 屬性。

範例

form_tag('/posts')
# => <form action="/posts" method="post">

form_tag('/posts/1', method: :put)
# => <form action="/posts/1" method="post"> ... <input name="_method" type="hidden" value="put" /> ...

form_tag('/upload', multipart: true)
# => <form action="/upload" method="post" enctype="multipart/form-data">

<%= form_tag('/posts') do -%>
  <div><%= submit_tag 'Save' %></div>
<% end -%>
# => <form action="/posts" method="post"><div><input type="submit" name="commit" value="Save" /></div></form>

<%= form_tag('/posts', remote: true) %>
# => <form action="/posts" method="post" data-remote="true">

form_tag(false, method: :get)
# => <form method="get">

form_tag('http://far.away.com/form', authenticity_token: false)
# form without authenticity token

form_tag('http://far.away.com/form', authenticity_token: "cf50faa3fe97702ca1ae")
# form with custom authenticity token
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 78
def form_tag(url_for_options = {}, options = {}, &block)
  html_options = html_options_for_form(url_for_options, options)
  if block_given?
    form_tag_with_body(html_options, capture(&block))
  else
    form_tag_html(html_options)
  end
end

hidden_field_tag(name, value = nil, options = {})

產生一個隱藏表單的輸入欄位,用來傳送資料,這些資料可能因為 HTTP 無狀態而導致遺失,或是一些應該對使用者隱藏的資料。

選項

  • 建立標籤的標準 HTML 屬性。

範例

hidden_field_tag 'tags_list'
# => <input type="hidden" name="tags_list" id="tags_list" autocomplete="off" />

hidden_field_tag 'token', 'VUBJKB23UIVI1UU1VOBVI@'
# => <input type="hidden" name="token" id="token" value="VUBJKB23UIVI1UU1VOBVI@" autocomplete="off" />

hidden_field_tag 'collected_input', '', onchange: "alert('Input collected!')"
# => <input type="hidden" name="collected_input" id="collected_input"
     value="" onchange="alert(&#39;Input collected!&#39;)" autocomplete="off" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 308
def hidden_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :hidden, autocomplete: "off"))
end

image_submit_tag(來源, 選項 = {})

顯示一張圖像,點選時會提交表單。

來源 傳遞給 AssetTagHelper#path_to_image

選項

  • :data - 此選項可用於新增自訂資料屬性。

  • :disabled - 若設為 true,使用者將無法使用此輸入。

  • 任何其他金鑰會為標籤建立標準 HTML 選項。

資料屬性

  • confirm: '問題?' - 這將新增一個 JavaScript 確認提示,內容為指定的問題。如果使用者驗證,表單會如常處理,否則不採取任何行動。

範例

image_submit_tag("login.png")
# => <input src="/assets/login.png" type="image" />

image_submit_tag("purchase.png", disabled: true)
# => <input disabled="disabled" src="/assets/purchase.png" type="image" />

image_submit_tag("search.png", class: 'search_button', alt: 'Find')
# => <input class="search_button" src="/assets/search.png" type="image" />

image_submit_tag("agree.png", disabled: true, class: "agree_disagree_button")
# => <input class="agree_disagree_button" disabled="disabled" src="/assets/agree.png" type="image" />

image_submit_tag("save.png", data: { confirm: "Are you sure?" })
# => <input src="/assets/save.png" data-confirm="Are you sure?" type="image" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 614
def image_submit_tag(source, options = {})
  options = options.stringify_keys
  src = path_to_image(source, skip_pipeline: options.delete("skip_pipeline"))
  tag :input, { "type" => "image", "src" => src }.update(options)
end

label_tag(名稱 = nil, 內容或選項 = nil, 選項 = nil, &區塊)

產生一個標籤元素。接受一個區塊。

選項

  • 建立標籤的標準 HTML 屬性。

範例

label_tag 'name'
# => <label for="name">Name</label>

label_tag 'name', 'Your name'
# => <label for="name">Your name</label>

label_tag 'name', nil, class: 'small_label'
# => <label for="name" class="small_label">Name</label>
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 281
def label_tag(name = nil, content_or_options = nil, options = nil, &block)
  if block_given? && content_or_options.is_a?(Hash)
    options = content_or_options = content_or_options.stringify_keys
  else
    options ||= {}
    options = options.stringify_keys
  end
  options["for"] = sanitize_to_id(name) unless name.blank? || options.has_key?("for")
  content_tag :label, content_or_options || name.to_s.humanize, options, &block
end

month_field_tag(名稱, 值 = nil, 選項 = {})

產生一個「月份」類型的文字欄位。

選項

支援與 text_field_tag 相同的選項。此外,還支援

  • :min - 可接受的最小值。

  • :max - 可接受的最大值。

  • :step - 可接受值的粒度。

範例

month_field_tag 'name'
# => <input id="name" name="name" type="month" />

month_field_tag 'month', '2014-01'
# => <input id="month" name="month" type="month" value="2014-01" />

month_field_tag 'month', nil, class: 'special_input'
# => <input class="special_input" id="month" name="month" type="month" />

month_field_tag 'month', '2014-01', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="month" name="month" type="month" value="2014-01" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 826
def month_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :month))
end

number_field_tag(名稱, 值 = nil, 選項 = {})

產生一個數字欄位。

選項

支援與 text_field_tag 相同的選項。此外,還支援

  • :min - 可接受的最小值。

  • :max - 可接受的最大值。

  • :in - 指定 :min:max 值的範圍。

  • :within - 與 :in 相同。

  • :step - 可接受值的粒度。

範例

number_field_tag 'quantity'
# => <input id="quantity" name="quantity" type="number" />

number_field_tag 'quantity', '1'
# => <input id="quantity" name="quantity" type="number" value="1" />

number_field_tag 'quantity', nil, class: 'special_input'
# => <input class="special_input" id="quantity" name="quantity" type="number" />

number_field_tag 'quantity', nil, min: 1
# => <input id="quantity" name="quantity" min="1" type="number" />

number_field_tag 'quantity', nil, max: 9
# => <input id="quantity" name="quantity" max="9" type="number" />

number_field_tag 'quantity', nil, in: 1...10
# => <input id="quantity" name="quantity" min="1" max="9" type="number" />

number_field_tag 'quantity', nil, within: 1...10
# => <input id="quantity" name="quantity" min="1" max="9" type="number" />

number_field_tag 'quantity', nil, min: 1, max: 10
# => <input id="quantity" name="quantity" min="1" max="10" type="number" />

number_field_tag 'quantity', nil, min: 1, max: 10, step: 2
# => <input id="quantity" name="quantity" min="1" max="10" step="2" type="number" />

number_field_tag 'quantity', '1', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="quantity" name="quantity" type="number" value="1" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 947
def number_field_tag(name, value = nil, options = {})
  options = options.stringify_keys
  options["type"] ||= "number"
  if range = options.delete("in") || options.delete("within")
    options.update("min" => range.min, "max" => range.max)
  end
  text_field_tag(name, value, options)
end

password_field_tag(名稱 = "password", 值 = nil, 選項 = {})

產生一個密碼欄位,一個遮罩文字欄位,將使用者的輸入隱藏在遮罩字元後面。

選項

  • :disabled - 若設為 true,使用者將無法使用此輸入。

  • :size - 輸入中可容納的可見字元數量。

  • :maxlength - 瀏覽器允許使用者輸入的最大字元數。

  • 任何其他金鑰都會建立標籤的標準 HTML 屬性。

範例

password_field_tag 'pass'
# => <input id="pass" name="pass" type="password" />

password_field_tag 'secret', 'Your secret here'
# => <input id="secret" name="secret" type="password" value="Your secret here" />

password_field_tag 'masked', nil, class: 'masked_input_field'
# => <input class="masked_input_field" id="masked" name="masked" type="password" />

password_field_tag 'token', '', size: 15
# => <input id="token" name="token" size="15" type="password" value="" />

password_field_tag 'key', nil, maxlength: 16
# => <input id="key" maxlength="16" name="key" type="password" />

password_field_tag 'confirm_pass', nil, disabled: true
# => <input disabled="disabled" id="confirm_pass" name="confirm_pass" type="password" />

password_field_tag 'pin', '1234', maxlength: 4, size: 6, class: "pin_input"
# => <input class="pin_input" id="pin" maxlength="4" name="pin" size="6" type="password" value="1234" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 380
def password_field_tag(name = "password", value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :password))
end

phone_field_tag(名稱, 值 = nil, 選項 = {})

radio_button_tag(名稱, 值, 選項 = {})
radio_button_tag(名稱, 值, 已勾選, 選項 = {})

產生一個單選按鈕;使用命名相同的單選按鈕,讓使用者從一組選項中選擇。

選項

  • :checked - 如果設定為 true,單選按鈕將會預設選取。

  • :disabled - 若設為 true,使用者將無法使用此輸入。

  • 任何其他金鑰會為標籤建立標準 HTML 選項。

範例

radio_button_tag 'favorite_color', 'maroon'
# => <input id="favorite_color_maroon" name="favorite_color" type="radio" value="maroon" />

radio_button_tag 'receive_updates', 'no', true
# => <input checked="checked" id="receive_updates_no" name="receive_updates" type="radio" value="no" />

radio_button_tag 'time_slot', "3:00 p.m.", false, disabled: true
# => <input disabled="disabled" id="time_slot_3:00_p.m." name="time_slot" type="radio" value="3:00 p.m." />

radio_button_tag 'color', "green", true, class: "color_input"
# => <input checked="checked" class="color_input" id="color_green" name="color" type="radio" value="green" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 493
def radio_button_tag(name, value, *args)
  if args.length >= 3
    raise ArgumentError, "wrong number of arguments (given #{args.length + 2}, expected 2..4)"
  end
  options = args.extract_options!
  checked = args.empty? ? false : args.first
  html_options = { "type" => "radio", "name" => name, "id" => "#{sanitize_to_id(name)}_#{sanitize_to_id(value)}", "value" => value }.update(options.stringify_keys)
  html_options["checked"] = "checked" if checked
  tag :input, html_options
end

range_field_tag(name, value = nil, options = {})

建立一個範圍表單元素。

選項

number_field_tag 支援相同的選項。

範例

range_field_tag 'quantity', '1'
# => <input id="quantity" name="quantity" type="range" value="1" />

range_field_tag 'quantity', in: 1...10
# => <input id="quantity" name="quantity" min="1" max="9" type="range" />

range_field_tag 'quantity', min: 1, max: 10, step: 2
# => <input id="quantity" name="quantity" min="1" max="10" step="2" type="range"
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 972
def range_field_tag(name, value = nil, options = {})
  number_field_tag(name, value, options.merge(type: :range))
end

search_field_tag(name, value = nil, options = {})

建立一個類型為「搜尋」的文字欄位。

選項

支援與 text_field_tag 相同的選項。

範例

search_field_tag 'name'
# => <input id="name" name="name" type="search" />

search_field_tag 'search', 'Enter your search query here'
# => <input id="search" name="search" type="search" value="Enter your search query here" />

search_field_tag 'search', nil, class: 'special_input'
# => <input class="special_input" id="search" name="search" type="search" />

search_field_tag 'search', 'Enter your search query here', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="search" name="search" type="search" value="Enter your search query here" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 691
def search_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :search))
end

select_tag(name, option_tags = nil, options = {})

建立一個下拉選單方塊,或者,如果 :multiple 選項設為 true,建立一個可多重選擇的方塊。

Helpers::FormOptions 能用來建立常見的選單方塊,例如國家、時區或相關記錄。option_tags 是一個包含選單方塊選項標籤的字串。

選項

  • :multiple - 如果設為 true,選項可允許多重選擇。

  • :disabled - 若設為 true,使用者將無法使用此輸入。

  • :include_blank - 如果設為 true,將建立一個空選項。如果設為字串,這個字串將會用做選項的內容,而值則會為空。

  • :prompt - 建立一個提示選項,具有空白值,以及要求使用者選擇某項文字的文字。

  • 任何其他金鑰都會建立標籤的標準 HTML 屬性。

範例

select_tag "people", options_from_collection_for_select(@people, "id", "name")
# <select id="people" name="people"><option value="1">David</option></select>

select_tag "people", options_from_collection_for_select(@people, "id", "name", "1")
# <select id="people" name="people"><option value="1" selected="selected">David</option></select>

select_tag "people", raw("<option>David</option>")
# => <select id="people" name="people"><option>David</option></select>

select_tag "count", raw("<option>1</option><option>2</option><option>3</option><option>4</option>")
# => <select id="count" name="count"><option>1</option><option>2</option>
#    <option>3</option><option>4</option></select>

select_tag "colors", raw("<option>Red</option><option>Green</option><option>Blue</option>"), multiple: true
# => <select id="colors" multiple="multiple" name="colors[]"><option>Red</option>
#    <option>Green</option><option>Blue</option></select>

select_tag "locations", raw("<option>Home</option><option selected='selected'>Work</option><option>Out</option>")
# => <select id="locations" name="locations"><option>Home</option><option selected='selected'>Work</option>
#    <option>Out</option></select>

select_tag "access", raw("<option>Read</option><option>Write</option>"), multiple: true, class: 'form_input', id: 'unique_id'
# => <select class="form_input" id="unique_id" multiple="multiple" name="access[]"><option>Read</option>
#    <option>Write</option></select>

select_tag "people", options_from_collection_for_select(@people, "id", "name"), include_blank: true
# => <select id="people" name="people"><option value="" label=" "></option><option value="1">David</option></select>

select_tag "people", options_from_collection_for_select(@people, "id", "name"), include_blank: "All"
# => <select id="people" name="people"><option value="">All</option><option value="1">David</option></select>

select_tag "people", options_from_collection_for_select(@people, "id", "name"), prompt: "Select something"
# => <select id="people" name="people"><option value="">Select something</option><option value="1">David</option></select>

select_tag "destination", raw("<option>NYC</option><option>Paris</option><option>Rome</option>"), disabled: true
# => <select disabled="disabled" id="destination" name="destination"><option>NYC</option>
#    <option>Paris</option><option>Rome</option></select>

select_tag "credit_card", options_for_select([ "VISA", "MasterCard" ], "MasterCard")
# => <select id="credit_card" name="credit_card"><option>VISA</option>
#    <option selected="selected">MasterCard</option></select>
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 201
def select_tag(name, option_tags = nil, options = {})
  option_tags ||= ""
  html_name = (options[:multiple] == true && !name.end_with?("[]")) ? "#{name}[]" : name

  if options.include?(:include_blank)
    include_blank = options[:include_blank]
    options = options.except(:include_blank)
    options_for_blank_options_tag = { value: "" }

    if include_blank == true
      include_blank = ""
      options_for_blank_options_tag[:label] = " "
    end

    if include_blank
      option_tags = content_tag("option", include_blank, options_for_blank_options_tag).safe_concat(option_tags)
    end
  end

  if prompt = options.delete(:prompt)
    option_tags = content_tag("option", prompt, value: "").safe_concat(option_tags)
  end

  content_tag "select", option_tags, { "name" => html_name, "id" => sanitize_to_id(name) }.update(options.stringify_keys)
end

submit_tag(value = "Save changes", options = {})

用文字 value 作為按鈕文字建立一個提交按鈕。

選項

  • :data - 此選項可用於新增自訂資料屬性。

  • :disabled - 如果是真的,使用者就無法使用此輸入。

  • 任何其他金鑰會為標籤建立標準 HTML 選項。

範例

submit_tag
# => <input name="commit" data-disable-with="Save changes" type="submit" value="Save changes" />

submit_tag "Edit this article"
# => <input name="commit" data-disable-with="Edit this article" type="submit" value="Edit this article" />

submit_tag "Save edits", disabled: true
# => <input disabled="disabled" name="commit" data-disable-with="Save edits" type="submit" value="Save edits" />

submit_tag nil, class: "form_submit"
# => <input class="form_submit" name="commit" type="submit" />

submit_tag "Edit", class: "edit_button"
# => <input class="edit_button" data-disable-with="Edit" name="commit" type="submit" value="Edit" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 527
def submit_tag(value = "Save changes", options = {})
  options = options.deep_stringify_keys
  tag_options = { "type" => "submit", "name" => "commit", "value" => value }.update(options)
  set_default_disable_with value, tag_options
  tag :input, tag_options
end

telephone_field_tag(name, value = nil, options = {})

建立一個類型為「tel」的文字欄位。

選項

支援與 text_field_tag 相同的選項。

範例

telephone_field_tag 'name'
# => <input id="name" name="name" type="tel" />

telephone_field_tag 'tel', '0123456789'
# => <input id="tel" name="tel" type="tel" value="0123456789" />

telephone_field_tag 'tel', nil, class: 'special_input'
# => <input class="special_input" id="tel" name="tel" type="tel" />

telephone_field_tag 'tel', '0123456789', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="tel" name="tel" type="tel" value="0123456789" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 714
def telephone_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :tel))
end

text_area_tag(name, content = nil, options = {})

別名:textarea_tag

text_field_tag(name, value = nil, options = {})

建立一個標準文字欄位;使用這些文字欄位可以輸入較小的文字區塊,例如使用者名稱或搜尋查詢。

選項

  • :disabled - 若設為 true,使用者將無法使用此輸入。

  • :size - 輸入中可容納的可見字元數量。

  • :maxlength - 瀏覽器允許使用者輸入的最大字元數。

  • :placeholder - 預設出現在欄位中的文字,當欄位取得焦點時會被移除。如果設為 true,將會使用在目前 I18n 語系中找到的翻譯內容(透過 helpers.placeholder.<modelname>.<attribute> )。

  • 任何其他金鑰都會建立標籤的標準 HTML 屬性。

範例

text_field_tag 'name'
# => <input id="name" name="name" type="text" />

text_field_tag 'query', 'Enter your search query here'
# => <input id="query" name="query" type="text" value="Enter your search query here" />

text_field_tag 'search', nil, placeholder: 'Enter search term...'
# => <input id="search" name="search" placeholder="Enter search term..." type="text" />

text_field_tag 'request', nil, class: 'special_input'
# => <input class="special_input" id="request" name="request" type="text" />

text_field_tag 'address', '', size: 75
# => <input id="address" name="address" size="75" type="text" value="" />

text_field_tag 'zip', nil, maxlength: 5
# => <input id="zip" maxlength="5" name="zip" type="text" />

text_field_tag 'payment_amount', '$0.00', disabled: true
# => <input disabled="disabled" id="payment_amount" name="payment_amount" type="text" value="$0.00" />

text_field_tag 'ip', '0.0.0.0', maxlength: 15, size: 20, class: "ip-input"
# => <input class="ip-input" id="ip" maxlength="15" name="ip" size="20" type="text" value="0.0.0.0" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 263
def text_field_tag(name, value = nil, options = {})
  tag :input, { "type" => "text", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
end

textarea_tag(name

建立一個文字輸入區塊;使用 textarea 來輸入較長的文字,例如部落格文章或描述。

選項

  • :size - 字串,指定 textarea 的維度(列數乘以列數),例如 "25x10"。

  • :rows - 指定 textarea 的列數

  • :cols - 指定 textarea 的列數

  • :disabled - 若設為 true,使用者將無法使用此輸入。

  • :escape - 預設情況下,文字輸入的內容會轉譯 HTML。如果不希望轉譯,請將此設定為 false。

  • 任何其他金鑰都會建立標籤的標準 HTML 屬性。

範例

textarea_tag 'post'
# => <textarea id="post" name="post"></textarea>

textarea_tag 'bio', @user.bio
# => <textarea id="bio" name="bio">This is my biography.</textarea>

textarea_tag 'body', nil, rows: 10, cols: 25
# => <textarea cols="25" id="body" name="body" rows="10"></textarea>

textarea_tag 'body', nil, size: "25x10"
# => <textarea name="body" id="body" cols="25" rows="10"></textarea>

textarea_tag 'description', "Description goes here.", disabled: true
# => <textarea disabled="disabled" id="description" name="description">Description goes here.</textarea>

textarea_tag 'comment', nil, class: 'comment_input'
# => <textarea class="comment_input" id="comment" name="comment"></textarea>
別名為:text_area_tag
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 413
def textarea_tag(name, content = nil, options = {})
  options = options.stringify_keys

  if size = options.delete("size")
    options["cols"], options["rows"] = size.split("x") if size.respond_to?(:split)
  end

  escape = options.delete("escape") { true }
  content = ERB::Util.html_escape(content) if escape

  content_tag :textarea, content.to_s.html_safe, { "name" => name, "id" => sanitize_to_id(name) }.update(options)
end

time_field_tag(name, value = nil, options = {})

建立類型為「time」的文字欄位。

選項

支援與 text_field_tag 相同的選項。此外,還支援

  • :min - 可接受的最小值。

  • :max - 可接受的最大值。

  • :step - 可接受值的粒度。

  • :include_seconds - 在輸出時間格式中包含秒數和毫秒(預設為 true)。

範例

time_field_tag 'name'
# => <input id="name" name="name" type="time" />

time_field_tag 'time', '01:01'
# => <input id="time" name="time" type="time" value="01:01" />

time_field_tag 'time', nil, class: 'special_input'
# => <input class="special_input" id="time" name="time" type="time" />

time_field_tag 'time', '01:01', include_seconds: true
# => <input id="time" name="time" type="time" value="01:01:00.000" />

time_field_tag 'time', '01:01', min: '00:00', max: '23:59', step: 1
# => <input id="time" max="23:59" min="00:00" name="time" step="1" type="time" value="01:01" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 769
def time_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :time))
end

url_field_tag(name, value = nil, options = {})

建立類型為「url」的文字欄位。

選項

支援與 text_field_tag 相同的選項。

範例

url_field_tag 'name'
# => <input id="name" name="name" type="url" />

url_field_tag 'url', 'http://rubyonrails.org'
# => <input id="url" name="url" type="url" value="http://rubyonrails.org" />

url_field_tag 'url', nil, class: 'special_input'
# => <input class="special_input" id="url" name="url" type="url" />

url_field_tag 'url', 'http://rubyonrails.org', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="url" name="url" type="url" value="http://rubyonrails.org" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 876
def url_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :url))
end

utf8_enforcer_tag()

建立隱藏的 UTF-8 編碼確認標籤。在 helper 中覆寫此方法以自訂標籤。

# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 978
def utf8_enforcer_tag
  # Use raw HTML to ensure the value is written as an HTML entity; it
  # needs to be the right character regardless of which encoding the
  # browser infers.
  '<input name="utf8" type="hidden" value="&#x2713;" autocomplete="off" />'.html_safe
end

week_field_tag(name, value = nil, options = {})

建立類型為「week」的文字欄位。

選項

支援與 text_field_tag 相同的選項。此外,還支援

  • :min - 可接受的最小值。

  • :max - 可接受的最大值。

  • :step - 可接受值的粒度。

範例

week_field_tag 'name'
# => <input id="name" name="name" type="week" />

week_field_tag 'week', '2014-W01'
# => <input id="week" name="week" type="week" value="2014-W01" />

week_field_tag 'week', nil, class: 'special_input'
# => <input class="special_input" id="week" name="week" type="week" />

week_field_tag 'week', '2014-W01', class: 'special_input', disabled: true
# => <input disabled="disabled" class="special_input" id="week" name="week" type="week" value="2014-W01" />
# File actionview/lib/action_view/helpers/form_tag_helper.rb, line 853
def week_field_tag(name, value = nil, options = {})
  text_field_tag(name, value, options.merge(type: :week))
end