Skip to content

Instantly share code, notes, and snippets.

@itssomething
Created August 14, 2018 04:01
Show Gist options
  • Save itssomething/0350c166e8c581981526636f39be0b45 to your computer and use it in GitHub Desktop.
Save itssomething/0350c166e8c581981526636f39be0b45 to your computer and use it in GitHub Desktop.
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's
// vendor/assets/javascripts directory can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file. JavaScript code in this file should be added after the last require_* statement.
//
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require rails-ujs
//= require activestorage
//= require turbolinks
//= require_tree .
//= require jquery3
//= require popper
//= require bootstrap-sprockets
$(document).on("turbolinks:load", function() {
$("form").on("click", ".add_fields", function(event) {
var regexp, time;
time = new Date().getTime();
regexp = new RegExp($(this).data("id"), "g");
$(".fields").append($(this).data("fields").replace(regexp, time));
return event.preventDefault() ;
});
$("form").on("click", ".remove_record", function(event) {
$(this).prev("input[type=hidden]").val("1");
$(this).closest("div.step").hide();
return event.preventDefault() ;
});
});
module ApplicationHelper
def link_to_add_row name, form, association, **args
new_object = form.object.send(association).klass.new
id = new_object.object_id
fields = form.fields_for(association, new_object, child_index: id) do |b|
render(association.to_s.singularize, f: b)
end
link_to(name, "#", class: "add_fields " + args[:class],
data: {id: id, fields: fields.delete("\n")})
end
end
<div class="row">
<div class="col-md-6 offset-md-3">
<%= form_for @question_bank do |f| %>
<%= f.label :name, t(".name"), class: "field-label" %>
<%= f.text_field :name, class: "form-control field-input" %>
<%= f.label :category, t(".category"), class: "field-label" %>
<h5>Add questions</h5>
<div>
<%= f.fields_for :questions do |ff| %>
<%= render "question", f: ff %>
<% end %>
</div>
<%= link_to_add_row(t(".add_question"), f, :questions, class: "btn btn-default") %>
<%= f.submit t(".submit"), class: "btn btn-primary submit-button" %>
<% end %>
</div>
<div>
<div class = "question">
<%= f.label :content, t(".content_question"), class: "field-label question-label" %>
<%= f.text_field :content, class: "form-control field-input" %>
<div>
<%= f.label :multi_correct, t(".multi"), class: "field-label" %>
<%= f.check_box :multi_correct, {}, "yes", "no" %>
</div>
<%= f.fields_for :answers do |ff| %>
<%= render "answer", f: ff %>
<% end %>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment