Skip to content

Instantly share code, notes, and snippets.

@Justinwceo
Created August 16, 2011 16:33
Show Gist options
  • Save Justinwceo/1149489 to your computer and use it in GitHub Desktop.
Save Justinwceo/1149489 to your computer and use it in GitHub Desktop.
Jquery Tokeinput & Dynamic Ruby on Rails Nested Form
<%= form_for(current_user, :url => user_products_path) do |f| %>
<%= f.error_messages %>
<% f.fields_for :user_products do |builder| %>
<%= render "user_product_fields", :f => builder %>
<% end %>
<p>
<%= link_to_add_fields "Additional User Product", f, :user_products %>
</p>
<%= f.submit "Create" %>
<% end %>
<div class="field">
<p>
<%= f.label :product_token, "Product" %>
<%= f.text_field :product_token, "data-pre" => @products.map(&:attributes).to_json %>
</p>
<p>
<%= f.label :address_token, "Address" %>
<%= f.text_field :address_token, "data-pre" => @business_addresses.map(&:attributes).to_json %>
</p>
<p>
<%= f.label :price %>
<%= f.text_field :price %>
</p>
<p>
<%= link_to_remove_fields "remove", f %>
</p>
</div>
// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(function() {
$("#product_token").tokenInput("/products.json", {
prePopulate: $("#product_token").data("pre"),
tokenLimit: 1
});
});
$(function() {
$("#address_token").tokenInput("/business_addresses.json", {
prePopulate: $("#address_token").data("pre"),
tokenLimit: 1
});
});
$(function() {
$("#business_token").tokenInput("/businesses.json", {
prePopulate: $("business_token").data("pre"),
tokenLimit: 1
});
});
function remove_fields(link) {
$(link).prev("input[type=hidden]").val("1");
$(link).closest(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
module ApplicationHelper
def link_to_remove_fields(name, f)
f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
end
def link_to_add_fields(name, f, association)
new_object = f.object.class.reflect_on_association(association).klass.new
fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
render(association.to_s.singularize + "_fields", :f => builder)
end
link_to_function(name, "add_fields(this, '#{association}', '#{escape_javascript(fields)}')" )
end
end
#Tokeninput cannot work because of the change of numbers, every time i add a new field the numbers change
id = user_user_products_attributes_1313511133617_product_token
id = user_user_products_attributes_1313511392917_product_token
id = user_user_products_attributes_1313511393973_product_token
#Below, even if I tried making it static but i believe its secretly still doing the same thing.
<p>
<%= f.label :product_token, "Product" %>
<%= f.text_field :product_token, "data-pre" => @products.map(&:attributes).to_json, :id => "product_token" %>
</p>
class User < ActiveRecord::Base
require 'digest/sha2'
attr_accessible :email, :first_name, :last_name, :nick_name, :password, :password_confirmation, :user_products_attributes
has_many :user_products
has_many :products, :through => :user_products
accepts_nested_attributes_for :user_products
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment