Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 982367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:41:07+00:00 2026-05-16T04:41:07+00:00

We’ve all seen the brilliant complex forms railscast where Ryan Bates explains how to

  • 0

We’ve all seen the brilliant complex forms railscast where Ryan Bates explains how to dynamically add or remove nested objects within the parent object form using Javascript.

Has anyone got any ideas about how these methods need to be modified so as to work with Haml Formtastic?

To add some context here’s a simplified version of the problem I’m currently facing:

# Teacher form (which has nested subject forms) [from my application]

- semantic_form_for(@teacher) do |form|
  - form.inputs do
    = form.input :first_name
    = form.input :surname
    = form.input :city
    = render 'subject_fields', :form => form 
    = link_to_add_fields "Add Subject", form, :subjects   

# Individual Subject form partial [from my application]

- form.fields_for :subjects do |ff| 
  #subject_field
    = ff.input :name
    = ff.input :exam
    = ff.input :level
    = ff.hidden_field :_destroy
    = link_to_remove_fields "Remove Subject", ff 

# Application Helper (straight from Railscasts)

  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 => "new_#{association}") do |builder|
      render(association.to_s.singularize + "_fields", :f => builder)
    end
    link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}  \")"))
  end

#Application.js (straight from Railscasts)

  function remove_fields(link) {
  $(link).previous("input[type=hidden]").value = "1";
  $(link).up(".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)
  });
  }

The problem with implementation seems to be with the javascript methods – the DOM tree of a Formtastic form differs greatly from a regular rails form.

I’ve seen this question asked online a few times but haven’t come across an answer yet – now you know that help will be appreciated by more than just me!

Jack

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:41:07+00:00Added an answer on May 16, 2026 at 4:41 am

    You’re on the right track:

    …the DOM tree of a Formtastic form
    differs greatly from a regular rails
    form.

    To adapt Ryan’s example for formtastic, it’s helpful to be reminded that the semantic_fields_for helper is similar to the semantic_form_for helper, which outputs the inputs in list form.

    To keep things as close to the Railscast code as possible, you’ll need to:

    • enclose the collection of nested fields in a wrapper (I use a div with the subjects CSS ID.)
    • enclose the nested fields in a ul/ol wrapper (I applied a nested-fields CSS class.)

    Here’s what your files should like.

    Teacher form (with nested Subject fields):

    - 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 => "subject_fields", :locals => { :f => builder }
          .links
            = link_to_add_fields "Add Subject", form, :subjects
    

    Subject fields partial (for nested Subject):

    %ul.nested-fields
      = f.input :name
      = f.input :exam
      = f.input :level
      = link_to_remove_fields "Remove Subject", f
    

    ApplicationHelper:

    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 => "new_#{association}") do |builder|
        render(association.to_s.singularize + "_fields", :f => builder)
      end
      link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
    end
    

    Application.js:

    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)
      });
    }
    

    Using following gems:

    • formtastic (0.9.10)
    • haml (3.0.2)
    • gherkin (1.0.26)
    • rails (2.3.5)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.