On Aug 10, 2:30 am, Rtdp [email protected] 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