# Request param type of array

**URL:** <https://www.ruby-forum.com/t/request-param-type-of-array/121963>\
**Category:** Rails\
**Created:** [December 3, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/request-param-type-of-array/121963 "2007-12-03T13:50:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [December 3, 2007, 1:50pm UTC](https://www.ruby-forum.com/t/request-param-type-of-array/121963/1 "2007-12-03T13:50:43Z")

</div>

Hello.

I have tested such form:

================  
After submitting my form:

\<% raise params.inspect %\>

{“myfield”=\>“value1”, “action”=\>“index”, “\_submit”=\>“OK”,  
“controller”=\>“params”}

\<% raise request.cgi.params.inspect %\>

{“myfield”=\>[“value1”, “value2”], “\_submit”=\>[“OK”]}

================

In Rails source code I have found such code:  
—\> actionpack-1.13.5/lib/action\_controller/cgi\_ext/cgi\_methods.rb  
(line 23):

```
def parse_request_parameters(params)
  parser = FormEncodedPairParser.new

  params = params.dup
  until params.empty?
    for key, value in params
      if key.blank?
        params.delete key
      elsif !key.include?('[')
        # much faster to test for the most common case first (GET)
        # and avoid the call to build_deep_hash
        parser.result[key] = get_typed_value(value[0])
        params.delete key
      elsif value.is_a?(Array)
        parser.parse(key, get_typed_value(value.shift))
        params.delete key if value.empty?
      else
        raise TypeError, "Expected array, found #{value.inspect}"
      end
    end
  end

  parser.result
end

```

=============

Why from cgi-param type of array (for array with length 2 and more)  
rails gets only first value?

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [December 3, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/request-param-type-of-array/121963/2 "2007-12-03T14:00:09Z")

</div>

Hi,

> 

try to use the name ‘myfield[]’. That way you are telling rails that you  
are expecting an array and that you want to access that param as such.  
After that it should be working as you’d expect.

Guess the rationale after that is that since there is no clear way to  
tell in plain html that you are going to use a multivalued field, you  
should be checking always at the backend if you have a single or  
multivalued param. Also you could get wrong results when having a  
duplicated name in a view (as is the case sometimes).

By forcing a special syntax, you can be sure you are getting what you  
are asking for. Even if you are finally passing a single value you can  
safely access it as an array.

regards,

javier ramírez

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [December 3, 2007, 2:13pm UTC](https://www.ruby-forum.com/t/request-param-type-of-array/121963/3 "2007-12-03T14:13:41Z")

</div>

I have found variant to send array of values and get them from params  
variable:

---

<div class="post-metadata">

**Author:** ![avanetix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/838e76/32.png) [@avanetix](https://www.ruby-forum.com/u/avanetix)\
**Post date:** [December 3, 2007, 2:46pm UTC](https://www.ruby-forum.com/t/request-param-type-of-array/121963/4 "2007-12-03T14:46:04Z")

</div>

Thanks 🙂
