# Dynamically creating the html for forms created by user in application

**URL:** <https://www.ruby-forum.com/t/dynamically-creating-the-html-for-forms-created-by-user-in-application/193036>\
**Category:** Rails\
**Created:** [August 10, 2010, 11:19am UTC](https://www.ruby-forum.com/t/dynamically-creating-the-html-for-forms-created-by-user-in-application/193036 "2010-08-10T11:19:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtdp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rtdp](https://www.ruby-forum.com/u/rtdp)\
**Post date:** [August 10, 2010, 11:19am UTC](https://www.ruby-forum.com/t/dynamically-creating-the-html-for-forms-created-by-user-in-application/193036/1 "2010-08-10T11:19:13Z")

</div>

hello,  
In my rails application, a user creates a form by selecting the type  
of the data i.e.(string, integer, text) and he then gives input filed  
name for it.  
i.e. if user wants Name as capition, txtname as input field name and  
string as database field type the form will generated as -

Name: 

So how can create this html from this user given information. Is there  
a plugin for this work ?

---

<div class="post-metadata">

**Author:** ![rtdp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rtdp](https://www.ruby-forum.com/u/rtdp)\
**Post date:** [August 10, 2010, 8:19pm UTC](https://www.ruby-forum.com/t/dynamically-creating-the-html-for-forms-created-by-user-in-application/193036/2 "2010-08-10T20:19:06Z")

</div>

On Aug 10, 2:30 am, Rtdp [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> So how can create this html from this user given information. Is there  
> a plugin for this work ?

This is a pretty common use case in lots of applications, and  
depending on what sorts of customization you want to allow your users,  
you can get quite complex with it if you like. But in the simple  
case, you basically want four model classes, divided roughly into a  
set of two for the form creators, and two for the users filling out  
the forms. Here’s an example using your terminology:

UserForm is a form created by a user  
FormField is a field on a UserForm, which contains a caption, a type,  
and a txtname  
UserForm has\_many :form\_fields

…and, parallel to these, for the users filling out the forms…

FormReply is one instance of a UserForm that’s been filled out  
ReplyValue is the answer the user gave to a particular FormField  
FormReply has\_many :reply\_values, and belongs\_to :user\_form  
(and UserForm has\_many :form\_replies)

When generating the HTML for this kind of thing, the key concept to  
keep in mind is that what the user’s actually doing is _creating a  
FormReply_. Thus, the ERB template might look like this:

\<%= form\_for(@form\_reply) do |f| %\>  
\<%= fields\_for :reply\_values do |value\_fields| %\>  
\<%= value\_fields.text\_field :value %\>  
\<% end %\>  
\<%= f.submit\_tag %\>  
\<% end %\>

The above template assumes you’ve set FormReply to accept nested  
attributes for reply\_values, and that you’re going to populate a blank  
set of them in the controller action.

Of course, this is only one way to do it, and I’m sure others have  
differing opinions on approaches… 🙂

Nat

---

<div class="post-metadata">

**Author:** ![rtdp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rtdp](https://www.ruby-forum.com/u/rtdp)\
**Post date:** [August 27, 2010, 6:26pm UTC](https://www.ruby-forum.com/t/dynamically-creating-the-html-for-forms-created-by-user-in-application/193036/3 "2010-08-27T18:26:21Z")

</div>

Ok, now i have implemented something similar to this from database  
perspective, but not from views and actual html generation thing. Will  
give  
try to above idea of for generating actual html using form\_for method  
itself.

Thanks Nat.

On Tue, Aug 10, 2010 at 11:48 PM, Nat B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> ## (and UserForm has\_many :form\_replies) \<% end %\>
> 
> You received this message because you are subscribed to the Google G.  
> “Ruby on Rails: Talk” group.  
> To post to this group, send email to removed\_email\_address@domain.invalid.  
> To unsubscribe from this group, send email to  
> removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> .  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Ratnadeep Deshmane.

> **[爽看1069gey男男视频,男生双性自慰桌角磨蹭花蒂,欧美贵妇xxxxxbbbb,经典三级人妻mv在线,曰韩亚洲av人人夜夜澡人人爽,无码人妻精品一区二区三区](http://www.rtdptech.com/index.php)**
>
> 爽看1069gey男男视频,男生双性自慰桌角磨蹭花蒂,欧美贵妇xxxxxbbbb,经典三级人妻mv在线,曰韩亚洲av人人夜夜澡人人爽,无码人妻精品一区二区三区,中国老头老太婆bbw视频

---

<div class="post-metadata">

**Author:** ![rtdp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71c47a/32.png) [@rtdp](https://www.ruby-forum.com/u/rtdp)\
**Post date:** [August 27, 2010, 6:40pm UTC](https://www.ruby-forum.com/t/dynamically-creating-the-html-for-forms-created-by-user-in-application/193036/4 "2010-08-27T18:40:24Z")

</div>

On a side note, if your application primarily relies on user generated  
forms, a documentbased/key-value database might be a better option.  
I’m talking about databases like MongoDB and the likes.

> **[Using Mongomapper to store dynamic data](https://www.slideshare.net/wonko/using-mongomapper-to-store-dynamic-data)**
>
> MONGODB AND RUBY Persisting dynamic data with MongoMapper MONGODB • humongous • auto-sharding • Speed!!! ...

As mentioned below, the whole stack for implementing dynamic fields in  
a relational database adds up to 4 or more models. If you want to  
retrieve all the data, you’ll end up with quite a few joins, which  
might impact performance quite a bit.

On 27 Aug 2010, at 18:24, Ratnadeep wrote:

> you can get quite complex with it if you like. But in the simple
> 
> Of course, this is only one way to do it, and I’m sure others have  
> differing opinions on approaches… 🙂

Best regards

Peter De Berdt
