Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're on the right track:</p> <blockquote> <p>...the DOM tree of a Formtastic form differs greatly from a regular rails form.</p> </blockquote> <p>To adapt Ryan's example for formtastic, it's helpful to be reminded that the <code>semantic_fields_for</code> helper is similar to the <code>semantic_form_for</code> helper, which outputs the inputs in list form.</p> <p>To keep things as close to the Railscast code as possible, you'll need to:</p> <ul> <li>enclose the collection of nested fields in a wrapper (I use a div with the <code>subjects</code> CSS ID.)</li> <li>enclose the nested fields in a ul/ol wrapper (I applied a <code>nested-fields</code> CSS class.)</li> </ul> <p>Here's what your files should like.</p> <p>Teacher form (with nested Subject fields):</p> <pre><code>- semantic_form_for(@teacher) do |form| - form.inputs do = form.input :first_name = form.input :surname = form.input :city %h2 Subjects #subjects - form.semantic_fields_for :subjects do |builder| = render :partial =&gt; "subject_fields", :locals =&gt; { :f =&gt; builder } .links = link_to_add_fields "Add Subject", form, :subjects </code></pre> <p>Subject fields partial (for nested Subject):</p> <pre><code>%ul.nested-fields = f.input :name = f.input :exam = f.input :level = link_to_remove_fields "Remove Subject", f </code></pre> <p>ApplicationHelper:</p> <pre><code>def link_to_remove_fields(name, f) f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)") end def link_to_add_fields(name, f, association) new_object = f.object.class.reflect_on_association(association).klass.new fields = f.fields_for(association, new_object, :child_index =&gt; "new_#{association}") do |builder| render(association.to_s.singularize + "_fields", :f =&gt; builder) end link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")")) end </code></pre> <p>Application.js:</p> <pre><code>function remove_fields(link) { $(link).previous("input[type=hidden]").value = "1"; $(link).up(".nested-fields").hide(); } function add_fields(link, association, content) { var new_id = new Date().getTime(); var regexp = new RegExp("new_" + association, "g") $(link).up().insert({ before: content.replace(regexp, new_id) }); } </code></pre> <p>Using following gems:</p> <ul> <li>formtastic (0.9.10)</li> <li>haml (3.0.2)</li> <li>gherkin (1.0.26)</li> <li>rails (2.3.5)</li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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