Skip to content

Instantly share code, notes, and snippets.

@ogryzek
Last active August 29, 2015 13:56

Revisions

  1. ogryzek revised this gist Feb 27, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions application_helper.rb
    Original file line number Diff line number Diff line change
    @@ -5,8 +5,8 @@ def link_to_add_fields(anchor_text, form_object, association)
    new_nested_object = form_object.object.send(association).new
    id = new_nested_object.object_id

    field_html = form_object.fields_for(association, new_nested_object, child_index: id) do |option|
    render(association.to_s.singularize + "_fields", f: option)
    field_html = form_object.fields_for(association, new_nested_object, child_index: id) do |ff|
    render(association.to_s.singularize + "_fields", f: ff)
    end

    link_to(anchor_text, "javascript:void(0);", class: "add_fields",
  2. ogryzek revised this gist Feb 27, 2014. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions questions.js.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    $(document).on "click", "form .add_fields", ->
    time = new Date().getTime()
    regex = new RegExp($(this).data("id"), "g")
    item_fields_html = $(this).data("field-html").replace(regex, time)
    $(this).before(item_fields_html)
  3. ogryzek revised this gist Feb 27, 2014. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions application_helper.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    module ApplicationHelper


    def link_to_add_fields(anchor_text, form_object, association)
    new_nested_object = form_object.object.send(association).new
    id = new_nested_object.object_id

    field_html = form_object.fields_for(association, new_nested_object, child_index: id) do |option|
    render(association.to_s.singularize + "_fields", f: option)
    end

    link_to(anchor_text, "javascript:void(0);", class: "add_fields",
    data: {id: id, field_html: field_html.gsub("\n", "")})
    end


    end
  4. ogryzek revised this gist Feb 27, 2014. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions _form.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    = form_for(@question) do |f|
    - if @question.errors.any?
    #error_explanation
    %h2
    = pluralize(@question.errors.count, "error")
    prohibited this question from being saved:
    %ul
    - @question.errors.full_messages.each do |msg|
    %li= msg
    .field
    = f.label :title
    %br
    = f.text_field :title
    .field
    = f.label :body
    %br
    = f.text_area :body
    %h3 Options
    = f.fields_for :options do |opt|
    = render "option_fields", f: opt

    = link_to_add_fields "Add Option", f, :options
    %br

    = f.submit
  5. ogryzek created this gist Feb 27, 2014.
    8 changes: 8 additions & 0 deletions _option_fields.html.haml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    %fieldset.answer
    = f.label :title
    = f.text_field :title
    = f.label :body
    = f.text_field :body
    = f.check_box :_destroy
    = f.label :_destroy
    %br