Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Idea in steps:</p> <p>1) Define a model such as AdditionalField (id, field_name, field_type, default_value, is_required)</p> <p>2) then create a function like:</p> <pre><code>def self.for_form(my_form_name = nil) if my_form_name.nil? self.all else self.find(:all, :contitions =&gt; {:form_type =&gt; my_form_name.type} # or whatever selection criteria end </code></pre> <p>3) then you can iterate over the found AdditionalFields and build the correct field types as needed.</p> <p>I used this solution for a comparison website where they needed to configure the questionnaires for each different comparison type.</p> <p>Here's the render code I used, you'll need to amend it to suit your situation. relationships are:</p> <pre><code>convention -&lt; booking &gt;- user convention -&lt; convention_question booking -&lt; guests guest -&lt; guest_answers </code></pre> <p>QuestionsHelper</p> <pre><code>def render_guest_questions(guest, convention_question) fields_for "booking[guest_answer_attributes][]", convention_question do |m| case convention_question.display_type when "Text" '&lt;td&gt;' + text_field_tag("booking[guest_answer_attributes][convention_question_#{guest.id}_#{convention_question.id}]") + '&lt;/td&gt;' when "Boolean" '&lt;td&gt;' + hidden_field_tag("booking[guest_answer_attributes][convention_question_#{guest.id}_#{convention_question.id}]", "No") + check_box_tag("booking[guest_answer_attributes][convention_question_#{guest.id}_#{convention_question.id}]", "Yes") + '&lt;/td&gt;' end end end </code></pre> <p>Controller</p> <pre><code># TURN GUEST/QUESTIONS INTO guest answers if params[:booking] &amp;&amp; !params[:booking].blank? &amp;&amp; !params[:booking][:guest_answer_attributes].blank? params[:booking][:guest_answer_attributes].each do |k,v| handle_answers(k, v) end end def handle_answers(k, v) x = k.mb_chars.split(/_/) g_id = x[2] q_id = x[3] item = GuestAnswer.find_or_create_by_guest_id_and_convention_question_id( {:guest_id =&gt; g_id, :convention_question_id =&gt; q_id, :answer =&gt; v}) end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload