Takehiro Adachi
Takehiro Adachi

Reputation: 984

Sending nested Array parameter in ruby on rails

Are there any way to send a 2D array parameter something like below

{ 'bar' => { 'foo' => [[10, 100], [20, 200], [30, 300]] } }

by using 6 number_field_tag ?

I tried:

<%= number_field_tag "bar[foo][][]" %>

But it didn't work

was 'bar' => { 'foo' => nil }

Upvotes: 1

Views: 1941

Answers (2)

Yunjun Wang
Yunjun Wang

Reputation: 81

I've had the same issue and I've solved it with the solution as below which suits any dimensions of arrays to be posted to Ruby on rails:

in your Ruby on rails create function: another way you can do is before saving, iterate through the params, get the value and push it to an new empty array and then set the params's array field with this new array, that'll do the job too.

(I have put comments as explanation)

@array_1 = params[:params][:array_1]  # suppose your params name is 
                                      # 'params' and you want to get 
                                      # the attribute in this json 
                                      # params 'array_1' which is an 
                                      # array, but Ruby rails will 
                                      # transform it to an object if 
                                      # not doing algorithms as below.
if @array_1
    array_1 = []
    @array_1.each do |a|
        @a = a[1] # here could be any number you want to get the 
                  # data you want to put in the array, puts the a 
                  # first to check it out
        if @child_array_1
           @child_array_1 = a[1]['child_array_1'] # suppose the name 
                                                 # is 'child_array_1' 
                                                 # in your json object 
                                                 # for the first child 
                                                 # array
           child_array_1 = []
           @child_array_1.each do |child|
           child_array_1.push(child[1])  # number 1 can be changed as 
                                         # you want as above array_1
        end
        a[1]['child_array_1'] = child_array_1
      end
      array_1.push(a[1])
    end
    @array_1 = array_1
    @params.array_1 = @array_1 # suppose the first array attribute in 
                               # your json object is 'array_1'
  end

Upvotes: 0

MCBama
MCBama

Reputation: 1490

Off the top of my head I can't think of a way to build a 2D array from 6 boxes, but what you can do is build 2 1D arrays and then combine them on the other end of whatever page you're visiting. For example:

<%= number_field_tag "bar[foo][]" %>
<%= number_field_tag "bar[foo][]" %>
<%= number_field_tag "bar[foo][]" %>
<%= number_field_tag "bar[foo2][]" %>
<%= number_field_tag "bar[foo2][]" %>
<%= number_field_tag "bar[foo2][]" %>

Builds a parameter list like so: "bar"=>{"foo"=>["10", "10", "10"], "foo2"=>["9", "9", "9"]}

Then on the receiving end it's a simple matter of combining the two with:

array = params[:bar][:foo].zip(params[:bar][:foo2])

which produces the following array:

[["10", "9"], ["10", "9"], ["10", "9"]]

As far as I'm aware there isn't a way to set up a 2D array in a params hash but i could be wrong. Hopefully this handles what you need :)

Upvotes: 1

Related Questions