# How to add fields dynamically?

**URL:** <https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787>\
**Category:** Rails\
**Created:** [January 28, 2009, 12:13am UTC](https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787 "2009-01-28T00:13:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tetya-loshad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tetya-loshad](https://www.ruby-forum.com/u/tetya-loshad)\
**Post date:** [January 28, 2009, 12:13am UTC](https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787/1 "2009-01-28T00:13:10Z")

</div>

I would like have a simple interface for users to add data into  
database. How to add new textareas in form, until user want it and  
submit all of them at once?

---

<div class="post-metadata">

**Author:** ![tetya-loshad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tetya-loshad](https://www.ruby-forum.com/u/tetya-loshad)\
**Post date:** [January 28, 2009, 12:46am UTC](https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787/2 "2009-01-28T00:46:04Z")

</div>

Aa Sh wrote:

> I would like have a simple interface for users to add data into  
> database. How to add new textareas in form, until user want it and  
> submit all of them at once?

However, if you _do_ need to look these strings up in the database, you  
need a  
child table with a parent\_id, and key and value fields!

---

<div class="post-metadata">

**Author:** ![tetya-loshad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tetya-loshad](https://www.ruby-forum.com/u/tetya-loshad)\
**Post date:** [January 28, 2009, 12:48am UTC](https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787/3 "2009-01-28T00:48:25Z")

</div>

On 28 Jan 2009, at 00:13, Aa Sh wrote:

> I would like have a simple interface for users to add data into  
> database. How to add new textareas in form, until user want it and  
> submit all of them at once?

> **[\#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73-complex-forms-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

> **[\#74 Complex Forms Part 2 - RailsCasts](http://railscasts.com/episodes/74-complex-forms-part-2)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

… and everything on [railscasts.com](http://railscasts.com) basically

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![tetya-loshad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tetya-loshad](https://www.ruby-forum.com/u/tetya-loshad)\
**Post date:** [January 28, 2009, 4:13pm UTC](https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787/4 "2009-01-28T16:13:56Z")

</div>

Peter De Berdt wrote:

> On 28 Jan 2009, at 00:13, Aa Sh wrote:
> 
> > I would like have a simple interface for users to add data into  
> > database. How to add new textareas in form, until user want it and  
> > submit all of them at once?
> 
> [#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73-complex-forms-part-1)  
> [#74 Complex Forms Part 2 - RailsCasts](http://railscasts.com/episodes/74-complex-forms-part-2)  
> … and everything on [railscasts.com](http://railscasts.com) basically
> 
> Best regards
> 
> Peter De Berdt

Thank you so much, it is almost exactly what I need, except I am a  
novice in RoR, so I can’t change the example from multi models to simple  
one. I try, but have an error.

Now I have table in database with “e\_key” and “comment” fields and  
standard new.html.erb (built by scaffolding). So actually I have two  
question:

1. How dynamically to add empty fields on page (by click button)?
2. How to collect all data from all rows and put they into database?

Thank you for your time!

---

<div class="post-metadata">

**Author:** ![tetya-loshad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tetya-loshad](https://www.ruby-forum.com/u/tetya-loshad)\
**Post date:** [January 28, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787/5 "2009-01-28T17:31:41Z")

</div>

You will need Javascript for this sort of thing as Ryan B. points out  
in his screen casts. There are many ways to do it, e.g., RJS templates  
or unobstrusive Javascript.

Next in your create action, you will have to loop through the newly  
added rows to the database.

It sounds simple, but it isn’t if you are a beginning Rails programmer.  
You may want to get a bit more experience with Javascript/Ajax before  
trying this. However, if you must, lookup the multi-model update recipe  
in Advanced Rails Recipes Book contributed by Ryan B. and just  
exclude the “one” side of “one-to-many” relationship.

Bharat

---

<div class="post-metadata">

**Author:** ![tetya-loshad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tetya-loshad](https://www.ruby-forum.com/u/tetya-loshad)\
**Post date:** [January 28, 2009, 6:02pm UTC](https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787/6 "2009-01-28T18:02:07Z")

</div>

Phlip, Peter, and Bharat,

Thank you for advises and your time!

---

<div class="post-metadata">

**Author:** ![tetya-loshad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tetya-loshad](https://www.ruby-forum.com/u/tetya-loshad)\
**Post date:** [January 28, 2009, 12:49am UTC](https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787/7 "2009-01-28T00:49:36Z")

</div>

Aa Sh wrote:

> I would like have a simple interface for users to add data into  
> database. How to add new textareas in form, until user want it and  
> submit all of them at once?

Look up how to serialize a Hash into a :text field.

If you don’t need to query a database by a field, you don’t need the  
full  
overhead of a field type. To give your users abstract data, let them  
enter keys  
and values into fields that expand forever, and then AR will serialize  
them.  
They go in as YAML strings of your hashes.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![tetya-loshad](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/67e7ee/32.png) [@tetya-loshad](https://www.ruby-forum.com/u/tetya-loshad)\
**Post date:** [January 30, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/how-to-add-fields-dynamically/158787/8 "2009-01-30T21:17:18Z")

</div>

Aa Sh wrote:

> Phlip, Peter, and Bharat,
> 
> Thank you for advises and your time!  
> I did it!
