actionview/lib/action_view/helpers/url_helper.rb
# Returns an array of hashes each containing :name and :value keys
# suitable for use as the names and values of form input fields:
#
#   to_form_params(name: 'David', nationality: 'Danish')
#   # => [{name: 'name', value: 'David'}, {name: 'nationality', value: 'Danish'}]
#
#   to_form_params(country: { name: 'Denmark' })
#   # => [{name: 'country[name]', value: 'Denmark'}]
#
#   to_form_params(countries: ['Denmark', 'Sweden']})
#   # => [{name: 'countries[]', value: 'Denmark'}, {name: 'countries[]', value: 'Sweden'}]
#
# An optional namespace can be passed to enclose key names:
#
#   to_form_params({ name: 'Denmark' }, 'country')
#   # => [{name: 'country[name]', value: 'Denmark'}]
Returns an array of hashes each containing :name and :value keys
suitable for use as the names and values of form input fields.







An optional namespace can be passed to enclose key names.


Returns an array of hashes each containing : name and : value keys suitable for use as the names and values of form input fields . An optional namespace can be passed to enclose key names .
def to_form_params(attribute, namespace = nil)
          attribute = if attribute.respond_to?(:permitted?)
            attribute.to_h
          else
            attribute
          end
          params = []
          case attribute
          when Hash
            attribute.each do |key, value|
              prefix = namespace ? "#{namespace}[#{key}]" : key
              params.push(*to_form_params(value, prefix))
            end
          when Array
            array_prefix = "#{namespace}[]"
            attribute.each do |value|
              params.push(*to_form_params(value, array_prefix))
            end
          else
            params << { name: namespace.to_s, value: attribute.to_param }
          end
          params.sort_by { |pair| pair[:name] }
        end
def to_form_params ( attribute , namespace = nil ) attribute = if attribute . respond_to? ( :permitted? ) attribute . to_h else attribute end params = [ ] case attribute when Hash attribute . each do | key , value | prefix = namespace ? "#{namespace}[#{key}]" : key params . push ( * to_form_params ( value , prefix ) ) end when Array array_prefix = "#{namespace}[]" attribute . each do | value | params . push ( * to_form_params ( value , array_prefix ) ) end else params << { name : namespace . to_s , value : attribute . to_param } end params . sort_by { | pair | pair [ :name ] } end
Returns an array of hashes each containing :name and :value keys
suitable for use as the names and values of form input fields:
Returns an array of hashes each containing : name and : value keys suitable for use as the names and values of form input fields :
attribute
<not_specific>
namespace
<not_specific>