Array field custom input

1,204 views
Skip to first unread message

Cam Allen

unread,
Sep 27, 2012, 1:38:59 PM9/27/12
to plataformate...@googlegroups.com
Hi,

I am trying to add a Mongoid array field to simple form. Currently it renders the array as a string "[ data, other_data ]" which is then sent back to the controller as a string not an array. 

I want to add my own custom input to render each input as it's own field with overridden input names. 


E.g. if the instance name is "test" and the array field name is "test_array" i want the each array item to have an input name such as "test[array][]".

I can't figure out how to override the attribute name unless i manually construct the "name" and override it in the html input options.

Any thoughts about how i can achieve this without hacking?

Thoughts appreciated.
cam

Rafael Mendonça França

unread,
Sep 27, 2012, 1:47:10 PM9/27/12
to plataformate...@googlegroups.com
The name generation is did by the Rails itself and as far I remember is not easy to extend the behavior without hacking.

Some related code:



The best way is manually construct the name.

Cam Allen

unread,
Sep 27, 2012, 3:22:14 PM9/27/12
to plataformate...@googlegroups.com
Rafael - Thanks for the prompt reply!

I thought as much but wanted to know if there was any simple form magic I could use.

I've tested and it works well. The array fields are reconstructed back in the controller params hash under the attribute field as an array of vales. I've created a gist outlining my solution: https://gist.github.com/3795872

Cheers again.
Cam

Rafael Mendonça França

unread,
Sep 27, 2012, 4:03:47 PM9/27/12
to plataformate...@googlegroups.com
Great! Thank to sharing the solution.

Cam Allen

unread,
Sep 29, 2012, 5:36:45 AM9/29/12
to plataformate...@googlegroups.com
Further to this if like me you use mongoid hash and array fields in a document, then you may need a custom simple form hash input which rails will reconstruct into an attribute hash in the controller. 

If so then i've added a gist, hopefully it's useful.

You just need to loop over the hash whilst setting the input HTML to have the required values (note i use the key as my field label here):

hash.each do |key, value|
  form_builder.input(field, :as => :hash, :input_html => { value: value, key: key }, :label => key)
end

Cheers,
Cam

William Franklin

unread,
Aug 6, 2015, 12:55:50 AM8/6/15
to SimpleForm
Very good! How do I change to make it work in the type collection fields?

Cam Allen

unread,
Aug 6, 2015, 5:42:49 AM8/6/15
to SimpleForm
The above gists were used to construct N text inputs from mongodb array / hash document fields.

I'm not following you question..can you provide some more details?

William Franklin

unread,
Aug 6, 2015, 1:50:04 PM8/6/15
to SimpleForm
Hello Cam, I would add array-type fields collection ...

Cam Allen

unread,
Aug 7, 2015, 4:34:23 AM8/7/15
to SimpleForm
Can't you just pass an array into the collection? Just construct the array values that you want.

From the docs..

f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries
Reply all
Reply to author
Forward
0 new messages