# frozen_string_literal: true
require "cgi"
require "action_view/helpers/date_helper"
require "action_view/helpers/url_helper"
require "action_view/helpers/form_tag_helper"
require "action_view/helpers/active_model_helper"
require "action_view/model_naming"
require "action_view/record_identifier"
require "active_support/core_ext/module/attribute_accessors"
require "active_support/core_ext/hash/slice"
require "active_support/core_ext/string/output_safety"
require "active_support/core_ext/string/inflections"
module ActionView
module Helpers # :nodoc:
# = Action View Form \Helpers
#
# Form helpers are designed to make working with resources much easier
# compared to using vanilla HTML.
#
# Typically, a form designed to create or update a resource reflects the
# identity of the resource in several ways: (i) the URL that the form is
# sent to (the form element's +action+ attribute) should result in a request
# being routed to the appropriate controller action (with the appropriate :id
# parameter in the case of an existing resource), (ii) input fields should
# be named in such a way that in the controller their values appear in the
# appropriate places within the +params+ hash, and (iii) for an existing record,
# when the form is initially displayed, input fields corresponding to attributes
# of the resource should show the current values of those attributes.
#
# In \Rails, this is usually achieved by creating the form using +form_for+ and
# a number of related helper methods. +form_for+ generates an appropriate form
# tag and yields a form builder object that knows the model the form is about.
# Input fields are created by calling methods defined on the form builder, which
# means they are able to generate the appropriate names and default values
# corresponding to the model attributes, as well as convenient IDs, etc.
# Conventions in the generated field names allow controllers to receive form data
# nicely structured in +params+ with no effort on your side.
#
# For example, to create a new person you typically set up a new instance of
# +Person+ in the PeopleController#new action, @person, and
# in the view template pass that object to +form_for+:
#
# <%= form_for @person do |f| %>
# <%= f.label :first_name %>:
# <%= f.text_field :first_name %>
#
# <%= f.label :last_name %>:
# <%= f.text_field :last_name %>
#
# <%= f.submit %>
# <% end %>
#
# The HTML generated for this would be (modulus formatting):
#
#
#
# label(:post, :cost)
# # =>
#
# label(:post, :title, "A short title")
# # =>
#
# label(:post, :title, "A short title", class: "title_label")
# # =>
#
# label(:post, :privacy, "Public Post", value: "public")
# # =>
#
# label(:post, :cost) do |translation|
# content_tag(:span, translation, class: "cost_label")
# end
# # =>
#
# label(:post, :cost) do |builder|
# content_tag(:span, builder.translation, class: "cost_label")
# end
# # =>
#
# label(:post, :terms) do
# raw('Accept Terms.')
# end
# # =>
def label(object_name, method, content_or_options = nil, options = nil, &block)
Tags::Label.new(object_name, method, self, content_or_options, options).render(&block)
end
# Returns an input tag of the "text" type tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
# shown.
#
# ==== Examples
# text_field(:post, :title, size: 20)
# # =>
#
# text_field(:post, :title, class: "create_input")
# # =>
#
# text_field(:post, :title, maxlength: 30, class: "title_input")
# # =>
#
# text_field(:session, :user, onchange: "if ($('#session_user').val() === 'admin') { alert('Your login cannot be admin!'); }")
# # =>
#
# text_field(:snippet, :code, size: 20, class: 'code_input')
# # =>
def text_field(object_name, method, options = {})
Tags::TextField.new(object_name, method, self, options).render
end
# Returns an input tag of the "password" type tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
# shown. For security reasons this field is blank by default; pass in a value via +options+ if this is not desired.
#
# ==== Examples
# password_field(:login, :pass, size: 20)
# # =>
#
# password_field(:account, :secret, class: "form_input", value: @account.secret)
# # =>
#
# password_field(:user, :password, onchange: "if ($('#user_password').val().length > 30) { alert('Your password needs to be shorter!'); }")
# # =>
#
# password_field(:account, :pin, size: 20, class: 'form_input')
# # =>
def password_field(object_name, method, options = {})
Tags::PasswordField.new(object_name, method, self, options).render
end
# Returns a hidden input tag tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
# shown.
#
# ==== Examples
# hidden_field(:signup, :pass_confirm)
# # =>
#
# hidden_field(:post, :tag_list)
# # =>
#
# hidden_field(:user, :token)
# # =>
def hidden_field(object_name, method, options = {})
Tags::HiddenField.new(object_name, method, self, options).render
end
# Returns a file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
# shown.
#
# Using this method inside a +form_for+ block will set the enclosing form's encoding to multipart/form-data.
#
# ==== Options
# * Creates standard HTML attributes for the tag.
# * :disabled - If set to true, the user will not be able to use this input.
# * :multiple - If set to true, *in most updated browsers* the user will be allowed to select multiple files.
# * :include_hidden - When multiple: true and include_hidden: true, the field will be prefixed with an field with an empty value to support submitting an empty collection of files.
# * :accept - If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations.
#
# ==== Examples
# file_field(:user, :avatar)
# # =>
#
# file_field(:post, :image, multiple: true)
# # =>
#
# file_field(:post, :attached, accept: 'text/html')
# # =>
#
# file_field(:post, :image, accept: 'image/png,image/gif,image/jpeg')
# # =>
#
# file_field(:attachment, :file, class: 'file_input')
# # =>
def file_field(object_name, method, options = {})
options = { include_hidden: multiple_file_field_include_hidden }.merge!(options)
Tags::FileField.new(object_name, method, self, convert_direct_upload_option_to_url(options.dup)).render
end
# Returns a textarea opening and closing tag set tailored for accessing a specified attribute (identified by +method+)
# on an object assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
# hash with +options+.
#
# ==== Examples
# text_area(:post, :body, cols: 20, rows: 40)
# # =>
#
# text_area(:comment, :text, size: "20x30")
# # =>
#
# text_area(:application, :notes, cols: 40, rows: 15, class: 'app_input')
# # =>
#
# text_area(:entry, :body, size: "20x20", disabled: 'disabled')
# # =>
def text_area(object_name, method, options = {})
Tags::TextArea.new(object_name, method, self, options).render
end
# Returns a checkbox tag tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). This object must be an instance object (@object) and not a local object.
# It's intended that +method+ returns an integer and if that integer is above zero, then the checkbox is checked.
# Additional options on the input tag can be passed as a hash with +options+. The +checked_value+ defaults to 1
# while the default +unchecked_value+ is set to 0 which is convenient for boolean values.
#
# ==== Options
#
# * Any standard HTML attributes for the tag can be passed in, for example +:class+.
# * :checked - +true+ or +false+ forces the state of the checkbox to be checked or not.
# * :include_hidden - If set to false, the auxiliary hidden field described below will not be generated.
#
# ==== Gotcha
#
# The HTML specification says unchecked check boxes are not successful, and
# thus web browsers do not send them. Unfortunately this introduces a gotcha:
# if an +Invoice+ model has a +paid+ flag, and in the form that edits a paid
# invoice the user unchecks its check box, no +paid+ parameter is sent. So,
# any mass-assignment idiom like
#
# @invoice.update(params[:invoice])
#
# wouldn't update the flag.
#
# To prevent this the helper generates an auxiliary hidden field before
# every check box. The hidden field has the same name and its
# attributes mimic an unchecked check box.
#
# This way, the client either sends only the hidden field (representing
# the check box is unchecked), or both fields. Since the HTML specification
# says key/value pairs have to be sent in the same order they appear in the
# form, and parameters extraction gets the last occurrence of any repeated
# key in the query string, that works for ordinary forms.
#
# Unfortunately that workaround does not work when the check box goes
# within an array-like parameter, as in
#
# <%= fields_for "project[invoice_attributes][]", invoice, index: nil do |form| %>
# <%= form.check_box :paid %>
# ...
# <% end %>
#
# because parameter name repetition is precisely what \Rails seeks to distinguish
# the elements of the array. For each item with a checked check box you
# get an extra ghost item with only that attribute, assigned to "0".
#
# In that case it is preferable to either use +check_box_tag+ or to use
# hashes instead of arrays.
#
# ==== Examples
#
# # Let's say that @post.validated? is 1:
# check_box("post", "validated")
# # =>
# #
#
# # Let's say that @puppy.gooddog is "no":
# check_box("puppy", "gooddog", {}, "yes", "no")
# # =>
# #
#
# check_box("eula", "accepted", { class: 'eula_check' }, "yes", "no")
# # =>
# #
def check_box(object_name, method, options = {}, checked_value = "1", unchecked_value = "0")
Tags::CheckBox.new(object_name, method, self, checked_value, unchecked_value, options).render
end
# Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
# radio button will be checked.
#
# To force the radio button to be checked pass checked: true in the
# +options+ hash. You may pass HTML options there as well.
#
# # Let's say that @post.category returns "rails":
# radio_button("post", "category", "rails")
# radio_button("post", "category", "java")
# # =>
# #
#
# # Let's say that @user.receive_newsletter returns "no":
# radio_button("user", "receive_newsletter", "yes")
# radio_button("user", "receive_newsletter", "no")
# # =>
# #
def radio_button(object_name, method, tag_value, options = {})
Tags::RadioButton.new(object_name, method, self, tag_value, options).render
end
# Returns a text_field of type "color".
#
# color_field("car", "color")
# # =>
def color_field(object_name, method, options = {})
Tags::ColorField.new(object_name, method, self, options).render
end
# Returns an input of type "search" for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object_name+). Inputs of type "search" may be styled differently by
# some browsers.
#
# search_field(:user, :name)
# # =>
# search_field(:user, :name, autosave: false)
# # =>
# search_field(:user, :name, results: 3)
# # =>
# # Assume request.host returns "www.example.com"
# search_field(:user, :name, autosave: true)
# # =>
# search_field(:user, :name, onsearch: true)
# # =>
# search_field(:user, :name, autosave: false, onsearch: true)
# # =>
# search_field(:user, :name, autosave: true, onsearch: true)
# # =>
def search_field(object_name, method, options = {})
Tags::SearchField.new(object_name, method, self, options).render
end
# Returns a text_field of type "tel".
#
# telephone_field("user", "phone")
# # =>
#
def telephone_field(object_name, method, options = {})
Tags::TelField.new(object_name, method, self, options).render
end
# aliases telephone_field
alias phone_field telephone_field
# Returns a text_field of type "date".
#
# date_field("user", "born_on")
# # =>
#
# The default value is generated by trying to call +strftime+ with "%Y-%m-%d"
# on the object's value, which makes it behave as expected for instances
# of DateTime and ActiveSupport::TimeWithZone. You can still override that
# by passing the "value" option explicitly, e.g.
#
# @user.born_on = Date.new(1984, 1, 27)
# date_field("user", "born_on", value: "1984-05-12")
# # =>
#
# You can create values for the "min" and "max" attributes by passing
# instances of Date or Time to the options hash.
#
# date_field("user", "born_on", min: Date.today)
# # =>
#
# Alternatively, you can pass a String formatted as an ISO8601 date as the
# values for "min" and "max."
#
# date_field("user", "born_on", min: "2014-05-20")
# # =>
#
def date_field(object_name, method, options = {})
Tags::DateField.new(object_name, method, self, options).render
end
# Returns a text_field of type "time".
#
# The default value is generated by trying to call +strftime+ with "%T.%L"
# on the object's value. If you pass include_seconds: false, it will be
# formatted by trying to call +strftime+ with "%H:%M" on the object's value.
# It is also possible to override this by passing the "value" option.
#
# ==== Options
#
# Supports the same options as FormTagHelper#time_field_tag.
#
# ==== Examples
#
# time_field("task", "started_at")
# # =>
#
# You can create values for the "min" and "max" attributes by passing
# instances of Date or Time to the options hash.
#
# time_field("task", "started_at", min: Time.now)
# # =>
#
# Alternatively, you can pass a String formatted as an ISO8601 time as the
# values for "min" and "max."
#
# time_field("task", "started_at", min: "01:00:00")
# # =>
#
# By default, provided times will be formatted including seconds. You can render just the hour
# and minute by passing include_seconds: false. Some browsers will render a simpler UI
# if you exclude seconds in the timestamp format.
#
# time_field("task", "started_at", value: Time.now, include_seconds: false)
# # =>
def time_field(object_name, method, options = {})
Tags::TimeField.new(object_name, method, self, options).render
end
# Returns a text_field of type "datetime-local".
#
# datetime_field("user", "born_on")
# # =>
#
# The default value is generated by trying to call +strftime+ with "%Y-%m-%dT%T"
# on the object's value, which makes it behave as expected for instances
# of DateTime and ActiveSupport::TimeWithZone.
#
# @user.born_on = Date.new(1984, 1, 12)
# datetime_field("user", "born_on")
# # =>
#
# You can create values for the "min" and "max" attributes by passing
# instances of Date or Time to the options hash.
#
# datetime_field("user", "born_on", min: Date.today)
# # =>
#
# Alternatively, you can pass a String formatted as an ISO8601 datetime as
# the values for "min" and "max."
#
# datetime_field("user", "born_on", min: "2014-05-20T00:00:00")
# # =>
#
# By default, provided datetimes will be formatted including seconds. You can render just the date, hour,
# and minute by passing include_seconds: false.
#
# @user.born_on = Time.current
# datetime_field("user", "born_on", include_seconds: false)
# # =>
def datetime_field(object_name, method, options = {})
Tags::DatetimeLocalField.new(object_name, method, self, options).render
end
alias datetime_local_field datetime_field
# Returns a text_field of type "month".
#
# month_field("user", "born_on")
# # =>
#
# The default value is generated by trying to call +strftime+ with "%Y-%m"
# on the object's value, which makes it behave as expected for instances
# of DateTime and ActiveSupport::TimeWithZone.
#
# @user.born_on = Date.new(1984, 1, 27)
# month_field("user", "born_on")
# # =>
#
def month_field(object_name, method, options = {})
Tags::MonthField.new(object_name, method, self, options).render
end
# Returns a text_field of type "week".
#
# week_field("user", "born_on")
# # =>
#
# The default value is generated by trying to call +strftime+ with "%Y-W%W"
# on the object's value, which makes it behave as expected for instances
# of DateTime and ActiveSupport::TimeWithZone.
#
# @user.born_on = Date.new(1984, 5, 12)
# week_field("user", "born_on")
# # =>
#
def week_field(object_name, method, options = {})
Tags::WeekField.new(object_name, method, self, options).render
end
# Returns a text_field of type "url".
#
# url_field("user", "homepage")
# # =>
#
def url_field(object_name, method, options = {})
Tags::UrlField.new(object_name, method, self, options).render
end
# Returns a text_field of type "email".
#
# email_field("user", "address")
# # =>
#
def email_field(object_name, method, options = {})
Tags::EmailField.new(object_name, method, self, options).render
end
# Returns an input tag of type "number".
#
# ==== Options
#
# Supports the same options as FormTagHelper#number_field_tag.
def number_field(object_name, method, options = {})
Tags::NumberField.new(object_name, method, self, options).render
end
# Returns an input tag of type "range".
#
# ==== Options
#
# Supports the same options as FormTagHelper#range_field_tag.
def range_field(object_name, method, options = {})
Tags::RangeField.new(object_name, method, self, options).render
end
def _object_for_form_builder(object) # :nodoc:
object.is_a?(Array) ? object.last : object
end
private
def html_options_for_form_with(url_for_options = nil, model = nil, html: {}, local: !form_with_generates_remote_forms,
skip_enforcing_utf8: nil, **options)
html_options = options.slice(:id, :class, :multipart, :method, :data, :authenticity_token).merge!(html)
html_options[:remote] = html.delete(:remote) || !local
html_options[:method] ||= :patch if model.respond_to?(:persisted?) && model.persisted?
if skip_enforcing_utf8.nil?
if options.key?(:enforce_utf8)
html_options[:enforce_utf8] = options[:enforce_utf8]
end
else
html_options[:enforce_utf8] = !skip_enforcing_utf8
end
html_options_for_form(url_for_options.nil? ? {} : url_for_options, html_options)
end
def instantiate_builder(record_name, record_object, options)
case record_name
when String, Symbol
object = record_object
object_name = record_name
else
object = record_name
object_name = model_name_from_record_or_class(object).param_key if object
end
builder = options[:builder] || default_form_builder_class
builder.new(object_name, object, self, options)
end
def default_form_builder_class
builder = default_form_builder || ActionView::Base.default_form_builder
builder.respond_to?(:constantize) ? builder.constantize : builder
end
end
# = Action View Form Builder
#
# A +FormBuilder+ object is associated with a particular model object and
# allows you to generate fields associated with the model object. The
# +FormBuilder+ object is yielded when using +form_for+ or +fields_for+.
# For example:
#
# <%= form_for @person do |person_form| %>
# Name: <%= person_form.text_field :name %>
# Admin: <%= person_form.check_box :admin %>
# <% end %>
#
# In the above block, a +FormBuilder+ object is yielded as the
# +person_form+ variable. This allows you to generate the +text_field+
# and +check_box+ fields by specifying their eponymous methods, which
# modify the underlying template and associates the @person model object
# with the form.
#
# The +FormBuilder+ object can be thought of as serving as a proxy for the
# methods in the +FormHelper+ module. This class, however, allows you to
# call methods with the model object you are building the form for.
#
# You can create your own custom FormBuilder templates by subclassing this
# class. For example:
#
# class MyFormBuilder < ActionView::Helpers::FormBuilder
# def div_radio_button(method, tag_value, options = {})
# @template.content_tag(:div,
# @template.radio_button(
# @object_name, method, tag_value, objectify_options(options)
# )
# )
# end
# end
#
# The above code creates a new method +div_radio_button+ which wraps a div
# around the new radio button. Note that when options are passed in, you
# must call +objectify_options+ in order for the model object to get
# correctly passed to the method. If +objectify_options+ is not called,
# then the newly created helper will not be linked back to the model.
#
# The +div_radio_button+ code from above can now be used as follows:
#
# <%= form_for @person, :builder => MyFormBuilder do |f| %>
# I am a child: <%= f.div_radio_button(:admin, "child") %>
# I am an adult: <%= f.div_radio_button(:admin, "adult") %>
# <% end -%>
#
# The standard set of helper methods for form building are located in the
# +field_helpers+ class attribute.
class FormBuilder
include ModelNaming
# The methods which wrap a form helper call.
class_attribute :field_helpers, default: [
:fields_for, :fields, :label, :text_field, :password_field,
:hidden_field, :file_field, :text_area, :check_box,
:radio_button, :color_field, :search_field,
:telephone_field, :phone_field, :date_field,
:time_field, :datetime_field, :datetime_local_field,
:month_field, :week_field, :url_field, :email_field,
:number_field, :range_field
]
attr_accessor :object_name, :object, :options
attr_reader :multipart, :index
alias :multipart? :multipart
def multipart=(multipart)
@multipart = multipart
if parent_builder = @options[:parent_builder]
parent_builder.multipart = multipart
end
end
def self._to_partial_path
@_to_partial_path ||= name.demodulize.underscore.sub!(/_builder$/, "")
end
def to_partial_path
self.class._to_partial_path
end
def to_model
self
end
def initialize(object_name, object, template, options)
@nested_child_index = {}
@object_name, @object, @template, @options = object_name, object, template, options
@default_options = @options ? @options.slice(:index, :namespace, :skip_default_ids, :allow_method_names_outside_object) : {}
@default_html_options = @default_options.except(:skip_default_ids, :allow_method_names_outside_object)
convert_to_legacy_options(@options)
if @object_name&.end_with?("[]")
if (object ||= @template.instance_variable_get("@#{@object_name[0..-3]}")) && object.respond_to?(:to_param)
@auto_index = object.to_param
else
raise ArgumentError, "object[] naming but object param and @object var don't exist or don't respond to to_param: #{object.inspect}"
end
end
@multipart = nil
@index = options[:index] || options[:child_index]
end
# Generate an HTML id attribute value.
#
# return the element's id attribute.
#
# <%= form_for @post do |f| %>
# <%# ... %>
#
# <% content_for :sticky_footer do %>
# <%= form.button(form: f.id) %>
# <% end %>
# <% end %>
#
# In the example above, the :sticky_footer content area will
# exist outside of the element. By declaring the
# form HTML attribute, we hint to the browser that the generated
# element should be treated as the
# element's submit button, regardless of where it exists in the DOM.
def id
options.dig(:html, :id) || options[:id]
end
# Generate an HTML id attribute value for the given field
#
# Return the value generated by the FormBuilder for the given
# attribute name.
#
# <%= form_for @post do |f| %>
# <%= f.label :title %>
# <%= f.text_field :title, aria: { describedby: f.field_id(:title, :error) } %>
# <%= tag.span("is blank", id: f.field_id(:title, :error) %>
# <% end %>
#
# In the example above, the element built by
# the call to FormBuilder#text_field declares an
# aria-describedby attribute referencing the
# element, sharing a common id root (post_title, in this
# case).
def field_id(method, *suffixes, namespace: @options[:namespace], index: @options[:index])
@template.field_id(@object_name, method, *suffixes, namespace: namespace, index: index)
end
# Generate an HTML name attribute value for the given name and
# field combination
#
# Return the value generated by the FormBuilder for the given
# attribute name.
#
# <%= form_for @post do |f| %>
# <%= f.text_field :title, name: f.field_name(:title, :subtitle) %>
# <%# => %>
# <% end %>
#
# <%= form_for @post do |f| %>
# <%= f.text_field :tag, name: f.field_name(:tag, multiple: true) %>
# <%# => %>
# <% end %>
#
def field_name(method, *methods, multiple: false, index: @options[:index])
object_name = @options.fetch(:as) { @object_name }
@template.field_name(object_name, method, *methods, index: index, multiple: multiple)
end
##
# :method: text_field
#
# :call-seq: text_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#text_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.text_field :name %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: password_field
#
# :call-seq: password_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#password_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.password_field :password %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: text_area
#
# :call-seq: text_area(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#text_area for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.text_area :detail %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: color_field
#
# :call-seq: color_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#color_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.color_field :favorite_color %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: search_field
#
# :call-seq: search_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#search_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.search_field :name %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: telephone_field
#
# :call-seq: telephone_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#telephone_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.telephone_field :phone %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: phone_field
#
# :call-seq: phone_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#phone_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.phone_field :phone %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: date_field
#
# :call-seq: date_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#date_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.date_field :born_on %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: time_field
#
# :call-seq: time_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#time_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.time_field :born_at %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: datetime_field
#
# :call-seq: datetime_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#datetime_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.datetime_field :graduation_day %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: datetime_local_field
#
# :call-seq: datetime_local_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#datetime_local_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.datetime_local_field :graduation_day %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: month_field
#
# :call-seq: month_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#month_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.month_field :birthday_month %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: week_field
#
# :call-seq: week_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#week_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.week_field :birthday_week %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: url_field
#
# :call-seq: url_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#url_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.url_field :homepage %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: email_field
#
# :call-seq: email_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#email_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.email_field :address %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: number_field
#
# :call-seq: number_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#number_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.number_field :age %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
##
# :method: range_field
#
# :call-seq: range_field(method, options = {})
#
# Wraps ActionView::Helpers::FormHelper#range_field for form builders:
#
# <%= form_with model: @user do |f| %>
# <%= f.range_field :age %>
# <% end %>
#
# Please refer to the documentation of the base helper for details.
(field_helpers - [:label, :check_box, :radio_button, :fields_for, :fields, :hidden_field, :file_field]).each do |selector|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{selector}(method, options = {}) # def text_field(method, options = {})
@template.public_send( # @template.public_send(
#{selector.inspect}, # :text_field,
@object_name, # @object_name,
method, # method,
objectify_options(options)) # objectify_options(options))
end # end
RUBY_EVAL
end
# Creates a scope around a specific model object like form_for, but
# doesn't create the form tags themselves. This makes fields_for suitable
# for specifying additional model objects in the same form.
#
# Although the usage and purpose of +fields_for+ is similar to +form_for+'s,
# its method signature is slightly different. Like +form_for+, it yields
# a FormBuilder object associated with a particular model object to a block,
# and within the block allows methods to be called on the builder to
# generate fields associated with the model object. Fields may reflect
# a model object in two ways - how they are named (hence how submitted
# values appear within the +params+ hash in the controller) and what
# default values are shown when the form the fields appear in is first
# displayed. In order for both of these features to be specified independently,
# both an object name (represented by either a symbol or string) and the
# object itself can be passed to the method separately -
#
# <%= form_for @person do |person_form| %>
# First name: <%= person_form.text_field :first_name %>
# Last name : <%= person_form.text_field :last_name %>
#
# <%= fields_for :permission, @person.permission do |permission_fields| %>
# Admin? : <%= permission_fields.check_box :admin %>
# <% end %>
#
# <%= person_form.submit %>
# <% end %>
#
# In this case, the checkbox field will be represented by an HTML +input+
# tag with the +name+ attribute permission[admin], and the submitted
# value will appear in the controller as params[:permission][:admin].
# If @person.permission is an existing record with an attribute
# +admin+, the initial state of the checkbox when first displayed will
# reflect the value of @person.permission.admin.
#
# Often this can be simplified by passing just the name of the model
# object to +fields_for+ -
#
# <%= fields_for :permission do |permission_fields| %>
# Admin?: <%= permission_fields.check_box :admin %>
# <% end %>
#
# ...in which case, if :permission also happens to be the name of an
# instance variable @permission, the initial state of the input
# field will reflect the value of that variable's attribute @permission.admin.
#
# Alternatively, you can pass just the model object itself (if the first
# argument isn't a string or symbol +fields_for+ will realize that the
# name has been omitted) -
#
# <%= fields_for @person.permission do |permission_fields| %>
# Admin?: <%= permission_fields.check_box :admin %>
# <% end %>
#
# and +fields_for+ will derive the required name of the field from the
# _class_ of the model object, e.g. if @person.permission, is
# of class +Permission+, the field will still be named permission[admin].
#
# Note: This also works for the methods in FormOptionsHelper and
# DateHelper that are designed to work with an object as base, like
# FormOptionsHelper#collection_select and DateHelper#datetime_select.
#
# +fields_for+ tries to be smart about parameters, but it can be confused if both
# name and value parameters are provided and the provided value has the shape of an
# option Hash. To remove the ambiguity, explicitly pass an option Hash, even if empty.
#
# <%= form_for @person do |person_form| %>
# ...
# <%= fields_for :permission, @person.permission, {} do |permission_fields| %>
# Admin?: <%= check_box_tag permission_fields.field_name(:admin), @person.permission[:admin] %>
# <% end %>
# ...
# <% end %>
#
# === Nested Attributes Examples
#
# When the object belonging to the current scope has a nested attribute
# writer for a certain attribute, fields_for will yield a new scope
# for that attribute. This allows you to create forms that set or change
# the attributes of a parent object and its associations in one go.
#
# Nested attribute writers are normal setter methods named after an
# association. The most common way of defining these writers is either
# with +accepts_nested_attributes_for+ in a model definition or by
# defining a method with the proper name. For example: the attribute
# writer for the association :address is called
# address_attributes=.
#
# Whether a one-to-one or one-to-many style form builder will be yielded
# depends on whether the normal reader method returns a _single_ object
# or an _array_ of objects.
#
# ==== One-to-one
#
# Consider a Person class which returns a _single_ Address from the
# address reader method and responds to the
# address_attributes= writer method:
#
# class Person
# def address
# @address
# end
#
# def address_attributes=(attributes)
# # Process the attributes hash
# end
# end
#
# This model can now be used with a nested fields_for, like so:
#
# <%= form_for @person do |person_form| %>
# ...
# <%= person_form.fields_for :address do |address_fields| %>
# Street : <%= address_fields.text_field :street %>
# Zip code: <%= address_fields.text_field :zip_code %>
# <% end %>
# ...
# <% end %>
#
# When address is already an association on a Person you can use
# +accepts_nested_attributes_for+ to define the writer method for you:
#
# class Person < ActiveRecord::Base
# has_one :address
# accepts_nested_attributes_for :address
# end
#
# If you want to destroy the associated model through the form, you have
# to enable it first using the :allow_destroy option for
# +accepts_nested_attributes_for+:
#
# class Person < ActiveRecord::Base
# has_one :address
# accepts_nested_attributes_for :address, allow_destroy: true
# end
#
# Now, when you use a form element with the _destroy parameter,
# with a value that evaluates to +true+, you will destroy the associated
# model (e.g. 1, '1', true, or 'true'):
#
# <%= form_for @person do |person_form| %>
# ...
# <%= person_form.fields_for :address do |address_fields| %>
# ...
# Delete: <%= address_fields.check_box :_destroy %>
# <% end %>
# ...
# <% end %>
#
# ==== One-to-many
#
# Consider a Person class which returns an _array_ of Project instances
# from the projects reader method and responds to the
# projects_attributes= writer method:
#
# class Person
# def projects
# [@project1, @project2]
# end
#
# def projects_attributes=(attributes)
# # Process the attributes hash
# end
# end
#
# Note that the projects_attributes= writer method is in fact
# required for fields_for to correctly identify :projects as a
# collection, and the correct indices to be set in the form markup.
#
# When projects is already an association on Person you can use
# +accepts_nested_attributes_for+ to define the writer method for you:
#
# class Person < ActiveRecord::Base
# has_many :projects
# accepts_nested_attributes_for :projects
# end
#
# This model can now be used with a nested fields_for. The block given to
# the nested fields_for call will be repeated for each instance in the
# collection:
#
# <%= form_for @person do |person_form| %>
# ...
# <%= person_form.fields_for :projects do |project_fields| %>
# <% if project_fields.object.active? %>
# Name: <%= project_fields.text_field :name %>
# <% end %>
# <% end %>
# ...
# <% end %>
#
# It's also possible to specify the instance to be used:
#
# <%= form_for @person do |person_form| %>
# ...
# <% @person.projects.each do |project| %>
# <% if project.active? %>
# <%= person_form.fields_for :projects, project do |project_fields| %>
# Name: <%= project_fields.text_field :name %>
# <% end %>
# <% end %>
# <% end %>
# ...
# <% end %>
#
# Or a collection to be used:
#
# <%= form_for @person do |person_form| %>
# ...
# <%= person_form.fields_for :projects, @active_projects do |project_fields| %>
# Name: <%= project_fields.text_field :name %>
# <% end %>
# ...
# <% end %>
#
# If you want to destroy any of the associated models through the
# form, you have to enable it first using the :allow_destroy
# option for +accepts_nested_attributes_for+:
#
# class Person < ActiveRecord::Base
# has_many :projects
# accepts_nested_attributes_for :projects, allow_destroy: true
# end
#
# This will allow you to specify which models to destroy in the
# attributes hash by adding a form element for the _destroy
# parameter with a value that evaluates to +true+
# (e.g. 1, '1', true, or 'true'):
#
# <%= form_for @person do |person_form| %>
# ...
# <%= person_form.fields_for :projects do |project_fields| %>
# Delete: <%= project_fields.check_box :_destroy %>
# <% end %>
# ...
# <% end %>
#
# When a collection is used you might want to know the index of each
# object in the array. For this purpose, the index method
# is available in the FormBuilder object.
#
# <%= form_for @person do |person_form| %>
# ...
# <%= person_form.fields_for :projects do |project_fields| %>
# Project #<%= project_fields.index %>
# ...
# <% end %>
# ...
# <% end %>
#
# Note that fields_for will automatically generate a hidden field
# to store the ID of the record. There are circumstances where this
# hidden field is not needed and you can pass include_id: false
# to prevent fields_for from rendering it automatically.
def fields_for(record_name, record_object = nil, fields_options = nil, &block)
fields_options, record_object = record_object, nil if fields_options.nil? && record_object.is_a?(Hash) && record_object.extractable_options?
fields_options ||= {}
fields_options[:builder] ||= options[:builder]
fields_options[:namespace] = options[:namespace]
fields_options[:parent_builder] = self
case record_name
when String, Symbol
if nested_attributes_association?(record_name)
return fields_for_with_nested_attributes(record_name, record_object, fields_options, block)
end
else
record_object = @template._object_for_form_builder(record_name)
record_name = model_name_from_record_or_class(record_object).param_key
end
object_name = @object_name
index = if options.has_key?(:index)
options[:index]
elsif defined?(@auto_index)
object_name = object_name.to_s.delete_suffix("[]")
@auto_index
end
record_name = if index
"#{object_name}[#{index}][#{record_name}]"
elsif record_name.end_with?("[]")
"#{object_name}[#{record_name[0..-3]}][#{record_object.id}]"
else
"#{object_name}[#{record_name}]"
end
fields_options[:child_index] = index
@template.fields_for(record_name, record_object, fields_options, &block)
end
# See the docs for the ActionView::Helpers::FormHelper#fields helper method.
def fields(scope = nil, model: nil, **options, &block)
options[:allow_method_names_outside_object] = true
options[:skip_default_ids] = !FormHelper.form_with_generates_ids
convert_to_legacy_options(options)
fields_for(scope || model, model, options, &block)
end
# Returns a label tag tailored for labelling an input field for a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). The text of label will default to the attribute name unless a translation
# is found in the current I18n locale (through helpers.label.
#
# label(:cost)
# # =>
#
# label(:title, "A short title")
# # =>
#
# label(:title, "A short title", class: "title_label")
# # =>
#
# label(:privacy, "Public Post", value: "public")
# # =>
#
# label(:cost) do |translation|
# content_tag(:span, translation, class: "cost_label")
# end
# # =>
#
# label(:cost) do |builder|
# content_tag(:span, builder.translation, class: "cost_label")
# end
# # =>
#
# label(:cost) do |builder|
# content_tag(:span, builder.translation, class: [
# "cost_label",
# ("error_label" if builder.object.errors.include?(:cost))
# ])
# end
# # =>
#
# label(:terms) do
# raw('Accept Terms.')
# end
# # =>
def label(method, text = nil, options = {}, &block)
@template.label(@object_name, method, text, objectify_options(options), &block)
end
# Returns a checkbox tag tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). This object must be an instance object (@object) and not a local object.
# It's intended that +method+ returns an integer and if that integer is above zero, then the checkbox is checked.
# Additional options on the input tag can be passed as a hash with +options+. The +checked_value+ defaults to 1
# while the default +unchecked_value+ is set to 0 which is convenient for boolean values.
#
# ==== Options
#
# * Any standard HTML attributes for the tag can be passed in, for example +:class+.
# * :checked - +true+ or +false+ forces the state of the checkbox to be checked or not.
# * :include_hidden - If set to false, the auxiliary hidden field described below will not be generated.
#
# ==== Gotcha
#
# The HTML specification says unchecked check boxes are not successful, and
# thus web browsers do not send them. Unfortunately this introduces a gotcha:
# if an +Invoice+ model has a +paid+ flag, and in the form that edits a paid
# invoice the user unchecks its check box, no +paid+ parameter is sent. So,
# any mass-assignment idiom like
#
# @invoice.update(params[:invoice])
#
# wouldn't update the flag.
#
# To prevent this the helper generates an auxiliary hidden field before
# every check box. The hidden field has the same name and its
# attributes mimic an unchecked check box.
#
# This way, the client either sends only the hidden field (representing
# the check box is unchecked), or both fields. Since the HTML specification
# says key/value pairs have to be sent in the same order they appear in the
# form, and parameters extraction gets the last occurrence of any repeated
# key in the query string, that works for ordinary forms.
#
# Unfortunately that workaround does not work when the check box goes
# within an array-like parameter, as in
#
# <%= fields_for "project[invoice_attributes][]", invoice, index: nil do |form| %>
# <%= form.check_box :paid %>
# ...
# <% end %>
#
# because parameter name repetition is precisely what \Rails seeks to distinguish
# the elements of the array. For each item with a checked check box you
# get an extra ghost item with only that attribute, assigned to "0".
#
# In that case it is preferable to either use +check_box_tag+ or to use
# hashes instead of arrays.
#
# ==== Examples
#
# # Let's say that @post.validated? is 1:
# check_box("validated")
# # =>
# #
#
# # Let's say that @puppy.gooddog is "no":
# check_box("gooddog", {}, "yes", "no")
# # =>
# #
#
# # Let's say that @eula.accepted is "no":
# check_box("accepted", { class: 'eula_check' }, "yes", "no")
# # =>
# #
def check_box(method, options = {}, checked_value = "1", unchecked_value = "0")
@template.check_box(@object_name, method, objectify_options(options), checked_value, unchecked_value)
end
# Returns a radio button tag for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). If the current value of +method+ is +tag_value+ the
# radio button will be checked.
#
# To force the radio button to be checked pass checked: true in the
# +options+ hash. You may pass HTML options there as well.
#
# # Let's say that @post.category returns "rails":
# radio_button("category", "rails")
# radio_button("category", "java")
# # =>
# #
#
# # Let's say that @user.receive_newsletter returns "no":
# radio_button("receive_newsletter", "yes")
# radio_button("receive_newsletter", "no")
# # =>
# #
def radio_button(method, tag_value, options = {})
@template.radio_button(@object_name, method, tag_value, objectify_options(options))
end
# Returns a hidden input tag tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
# shown.
#
# ==== Examples
# # Let's say that @signup.pass_confirm returns true:
# hidden_field(:pass_confirm)
# # =>
#
# # Let's say that @post.tag_list returns "blog, ruby":
# hidden_field(:tag_list)
# # =>
#
# # Let's say that @user.token returns "abcde":
# hidden_field(:token)
# # =>
#
def hidden_field(method, options = {})
@emitted_hidden_id = true if method == :id
@template.hidden_field(@object_name, method, objectify_options(options))
end
# Returns a file upload input tag tailored for accessing a specified attribute (identified by +method+) on an object
# assigned to the template (identified by +object+). Additional options on the input tag can be passed as a
# hash with +options+. These options will be tagged onto the HTML as an HTML element attribute as in the example
# shown.
#
# Using this method inside a +form_with+ block will set the enclosing form's encoding to multipart/form-data.
#
# ==== Options
# * Creates standard HTML attributes for the tag.
# * :disabled - If set to true, the user will not be able to use this input.
# * :multiple - If set to true, *in most updated browsers* the user will be allowed to select multiple files.
# * :include_hidden - When multiple: true and include_hidden: true, the field will be prefixed with an field with an empty value to support submitting an empty collection of files. Since include_hidden will default to config.active_storage.multiple_file_field_include_hidden if you don't specify include_hidden, you will need to pass include_hidden: false to prevent submitting an empty collection of files when passing multiple: true.
# * :accept - If set to one or multiple mime-types, the user will be suggested a filter when choosing a file. You still need to set up model validations.
#
# ==== Examples
# # Let's say that @user has avatar:
# file_field(:avatar)
# # =>
#
# # Let's say that @post has image:
# file_field(:image, :multiple => true)
# # =>
#
# # Let's say that @post has attached:
# file_field(:attached, accept: 'text/html')
# # =>
#
# # Let's say that @post has image:
# file_field(:image, accept: 'image/png,image/gif,image/jpeg')
# # =>
#
# # Let's say that @attachment has file:
# file_field(:file, class: 'file_input')
# # =>
def file_field(method, options = {})
self.multipart = true
@template.file_field(@object_name, method, objectify_options(options))
end
# Add the submit button for the given form. When no value is given, it checks
# if the object is a new resource or not to create the proper label:
#
# <%= form_for @post do |f| %>
# <%= f.submit %>
# <% end %>
#
# In the example above, if @post is a new record, it will use "Create Post" as
# submit button label; otherwise, it uses "Update Post".
#
# Those labels can be customized using I18n under the +helpers.submit+ key and using
# %{model} for translation interpolation:
#
# en:
# helpers:
# submit:
# create: "Create a %{model}"
# update: "Confirm changes to %{model}"
#
# It also searches for a key specific to the given object:
#
# en:
# helpers:
# submit:
# post:
# create: "Add %{model}"
#
def submit(value = nil, options = {})
value, options = nil, value if value.is_a?(Hash)
value ||= submit_default_value
@template.submit_tag(value, options)
end
# Add the submit button for the given form. When no value is given, it checks
# if the object is a new resource or not to create the proper label:
#
# <%= form_for @post do |f| %>
# <%= f.button %>
# <% end %>
#
# In the example above, if @post is a new record, it will use "Create Post" as
# button label; otherwise, it uses "Update Post".
#
# Those labels can be customized using I18n under the +helpers.submit+ key
# (the same as submit helper) and using %{model} for translation interpolation:
#
# en:
# helpers:
# submit:
# create: "Create a %{model}"
# update: "Confirm changes to %{model}"
#
# It also searches for a key specific to the given object:
#
# en:
# helpers:
# submit:
# post:
# create: "Add %{model}"
#
# ==== Examples
# button("Create post")
# # =>
#
# button(:draft, value: true)
# # =>
#
# button do
# content_tag(:strong, 'Ask me!')
# end
# # =>
#
# button do |text|
# content_tag(:strong, text)
# end
# # =>
#
# button(:draft, value: true) do
# content_tag(:strong, "Save as draft")
# end
# # =>
#
def button(value = nil, options = {}, &block)
case value
when Hash
value, options = nil, value
when Symbol
value, options = nil, { name: field_name(value), id: field_id(value) }.merge!(options.to_h)
end
value ||= submit_default_value
if block_given?
value = @template.capture { yield(value) }
end
formmethod = options[:formmethod]
if formmethod.present? && !/post|get/i.match?(formmethod) && !options.key?(:name) && !options.key?(:value)
options.merge! formmethod: :post, name: "_method", value: formmethod
end
@template.button_tag(value, options)
end
def emitted_hidden_id? # :nodoc:
@emitted_hidden_id ||= nil
end
private
def objectify_options(options)
result = @default_options.merge(options)
result[:object] = @object
result
end
def submit_default_value
object = convert_to_model(@object)
key = object ? (object.persisted? ? :update : :create) : :submit
model = if object.respond_to?(:model_name)
object.model_name.human
else
@object_name.to_s.humanize
end
defaults = []
# Object is a model and it is not overwritten by as and scope option.
if object.respond_to?(:model_name) && object_name.to_s == model.downcase
defaults << :"helpers.submit.#{object.model_name.i18n_key}.#{key}"
else
defaults << :"helpers.submit.#{object_name}.#{key}"
end
defaults << :"helpers.submit.#{key}"
defaults << "#{key.to_s.humanize} #{model}"
I18n.t(defaults.shift, model: model, default: defaults)
end
def nested_attributes_association?(association_name)
@object.respond_to?("#{association_name}_attributes=")
end
def fields_for_with_nested_attributes(association_name, association, options, block)
name = "#{object_name}[#{association_name}_attributes]"
association = convert_to_model(association)
if association.respond_to?(:persisted?)
association = [association] if @object.public_send(association_name).respond_to?(:to_ary)
elsif !association.respond_to?(:to_ary)
association = @object.public_send(association_name)
end
if association.respond_to?(:to_ary)
explicit_child_index = options[:child_index]
output = ActiveSupport::SafeBuffer.new
association.each do |child|
if explicit_child_index
options[:child_index] = explicit_child_index.call if explicit_child_index.respond_to?(:call)
else
options[:child_index] = nested_child_index(name)
end
if content = fields_for_nested_model("#{name}[#{options[:child_index]}]", child, options, block)
output << content
end
end
output
elsif association
fields_for_nested_model(name, association, options, block)
end
end
def fields_for_nested_model(name, object, fields_options, block)
object = convert_to_model(object)
emit_hidden_id = object.persisted? && fields_options.fetch(:include_id) {
options.fetch(:include_id, true)
}
@template.fields_for(name, object, fields_options) do |f|
output = @template.capture(f, &block)
output.concat f.hidden_field(:id) if output && emit_hidden_id && !f.emitted_hidden_id?
output
end
end
def nested_child_index(name)
@nested_child_index[name] ||= -1
@nested_child_index[name] += 1
end
def convert_to_legacy_options(options)
if options.key?(:skip_id)
options[:include_id] = !options.delete(:skip_id)
end
end
end
end
ActiveSupport.on_load(:action_view) do
cattr_accessor :default_form_builder, instance_writer: false, instance_reader: false, default: ::ActionView::Helpers::FormBuilder
end
end