Sign in through GitHub

Please read for an updated status on RailsCasts:

Learn more or hide this

Lev Brie's Profile

GitHub User: levbrie

Comments by Lev Brie

Avatar

This is the default behavior when clicking on a link with an href="#". Make sure that whatever javascript your using to add or remove fields includes an event.preventDefault() call. In coffeescript, this should look something like the following for nested fields but can easily be applied to any click event where you need to suppress the default behavior for a given event:

app/assets/javascripts/nested_forms.js.coffee
jQuery ->
  $('form').on 'click', '.remove_fields', (event) ->
    $(this).prev('input[type=hidden]').val('1')
    $(this).closest('fieldset').hide()
    event.preventDefault()

  $('form').on 'click', '.add_fields', (event) ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()