A
A
Askhat Bikmetov2014-04-28 17:05:21
ruby
Askhat Bikmetov, 2014-04-28 17:05:21

How to pass input values ​​to Array field of MongoDB in Sinatra using Mongoid?

The view has this code:

%form{"method" => "post", "action" => "/order"}
  %input{"name" => "firstname"}
  %input{"name" => "order", "value" => "burger", "type" => "checkbox"}
  %input{"name" => "order", "value" => "nuggets", "type" => "checkbox"}
  %input{"name" => "order", "value" => "mtdew", "type" => "checkbox"}
  %input{"type" => "submit"}

And, accordingly, this action:
post "/order" do
  Order.new(
    :name => params['name'],
    :order => params['order']
  ).save
end

Now if you look at the database, you can see that :name was passed correctly, but :order only recorded the last value.
I understand that the action is wrong, but I have no idea how to pass an array to a field.
Thanks in advance for your replies.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Arkady Butermanov, 2014-04-28
@askhat

You entered the field name incorrectly. If you require multiple selection, then the field name must end with [] :

...
 %input{"name" => "order[]", "value" => "burger", "type" => "checkbox"}
  %input{"name" => "order[]", "value" => "nuggets", "type" => "checkbox"}
  %input{"name" => "order[]", "value" => "mtdew", "type" => "checkbox"}
...

In this case, params['order'] will contain an array, not a string.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question