Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically add fields in rails with out nested attributes
    primarykey
    data
    text
    <p>I am in the early stages of creating an app, and am just putting some basic code in place. Here is the current code...</p> <h1>app/views/cards/front.html.erb</h1> <pre><code>&lt;%= form_for(front_of_card_path) do |f| %&gt; &lt;%= f.fields_for :competency_templates do |builder| %&gt; &lt;%= render 'add_fields', f: builder %&gt; &lt;% end %&gt; &lt;%= link_to_add_fields "Add New Tag", f, :skill %&gt; &lt;% end %&gt; </code></pre> <h1>routes</h1> <pre><code> 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 </code></pre> <h1>controller</h1> <pre><code> 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 &amp; tags end </code></pre> <h1>cards.js.coffee</h1> <pre><code>jQuery -&gt; $('form').on 'click', '.remove_fields', (event) -&gt; $(this).prev('input[type=hidden]').val('1') $(this).closest('fieldset').hide() event.preventDefault() $('form').on 'click', '.add_fields', (event) -&gt; time = new Date().getTime() regexp = new RegExp($(this).data('id'), 'g') $(this).before($(this).data('fields').replace(regexp, time)) event.preventDefault() </code></pre> <h1>app_helper</h1> <pre><code>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 </code></pre> <p>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.</p> <h1>Update</h1> <p>Added more code to flesh this out. The issue I am having is the 3rd variable I am passing in on this line...</p> <pre><code> &lt;%= link_to_add_fields "Add New Tag", f, :skill %&gt; </code></pre> <p>It does not like ':skill', but I am not sure what I should be passing here.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload