I am in the early stages of creating an app, and am just putting some basic code in place. Here is the current code...
app/views/cards/front.html.erb
<%= form_for(front_of_card_path) do |f| %> <%= f.fields_for :competency_templates do |builder| %> <%= render 'add_fields', f: builder %> <% end %> <%= link_to_add_fields "Add New Tag", f, :skill %> <% end %>
routes
controller :cards do get '/front', action: 'front', as: 'front_of_card' post '/save', action: 'create', as: 'save_card' get '/my_contact_info', action: 'back', as: 'back_of_card' put '/save', action: 'update', as: 'save_card' get '/my_card', action: 'show', as: 'card' end
controller
def create @skill= Skill.new(params[:skill]) @tag = Tag.new(params[:tag]) @tag.save @skill.tag_id = @tag.id @skill.save redirect_to front_of_card_path, notice: 'Skill was successfully created.' #get user/session #save skills & tags end
cards.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()
app_helper
module ApplicationHelper def link_to_add_fields(name, f, association) new_object = f.object.send(association).klass.new id = new_object.object_id fields = f.fields_for(association, new_object, child_index: id) do |builder| render(association.to_s.singularize + "_fields", f: builder) end link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")}) end end
So right now this code gives me two text fields. One for the a tag name and another for a tag weight, and the controller inserts everything in the DB. I would like use some javascript to dynamically add as many of these tag/weight fields as I like. Everything I've found seems to focus on nested attributes. Any ideas appreciated.
Update
Added more code to flesh this out. The issue I am having is the 3rd variable I am passing in on this line...
<%= link_to_add_fields "Add New Tag", f, :skill %>
It does not like ':skill', but I am not sure what I should be passing here.
after()
to add another div with the copied html.