How to add fields dynamically?

46 views
Skip to first unread message

Aa Sh

unread,
Jan 27, 2009, 6:13:10 PM1/27/09
to rubyonra...@googlegroups.com
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?
--
Posted via http://www.ruby-forum.com/.

Phlip

unread,
Jan 27, 2009, 6:40:34 PM1/27/09
to rubyonra...@googlegroups.com
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

Phlip

unread,
Jan 27, 2009, 6:41:09 PM1/27/09
to rubyonra...@googlegroups.com
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!

Peter De Berdt

unread,
Jan 27, 2009, 6:47:43 PM1/27/09
to rubyonra...@googlegroups.com

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?

Aa Sh

unread,
Jan 28, 2009, 10:13:56 AM1/28/09
to rubyonra...@googlegroups.com

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!

Bharat Ruparel

unread,
Jan 28, 2009, 11:31:41 AM1/28/09
to rubyonra...@googlegroups.com
You will need Javascript for this sort of thing as Ryan Bates 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 Bates and just
exclude the "one" side of "one-to-many" relationship.

Bharat

Aa Sh

unread,
Jan 28, 2009, 12:02:07 PM1/28/09
to rubyonra...@googlegroups.com
Phlip, Peter, and Bharat,

Thank you for advises and your time!

Aa Sh

unread,
Jan 30, 2009, 3:17:18 PM1/30/09
to rubyonra...@googlegroups.com
Aa Sh wrote:
> Phlip, Peter, and Bharat,
>
> Thank you for advises and your time!
I did it!
Reply all
Reply to author
Forward
0 new messages