0

I am working with form_tags in rails and using MongoDB for my application.

Using the form_tag i am getting params[:ids] using post method like this

["5262353ebd521b131a000010", "526b47f8bd521bd1b3000002"]

And i am saving this params[:id] as hidden_field as

<%= f.hidden_field :ids, :value => params[:ids].join(","), :multiple => true %>

But its storing in the MongoDB like this

"ids" : [
        "5262353ebd521b131a000010,526b47f8bd521bd1b3000002"
    ],

But instead of this i need to store as,

"ids" : [
        "5262353ebd521b131a000010","526b47f8bd521bd1b3000002"
    ],

with double quote, For single id its working fine. For multiple values its not storing as above.

Please help me in this regard.

4

3 回答 3

1

You could create a hidden field for each value so that when posting to your controller you get it as an array:

<% params[:ids].each do |id| %>
  <%= hidden_field_tag "ids[]", id %>
<% end %>

With that, you'll get an array in your action and you can directly store params[:ids] with your model.

于 2013-10-26T06:35:46.417 回答
1

try for this <%= f.hidden_field :ids, :value => params[:ids].join('","'), :multiple => true %>

于 2013-10-26T06:45:54.103 回答
0
<% params[:ids].each do |id| %>
   <%= f.hidden_field :ids, :value => id, :multiple => :true %>
<% end %>

This works and my MongoDB is,

   "ids" : [
        ObjectId("526b47f8bd521bd1b3000002"),
        ObjectId("5262353ebd521b131a000010")
    ],

Cheers!!!
于 2013-10-26T06:58:39.057 回答