Created
August 16, 2011 16:33
-
-
Save Justinwceo/1149489 to your computer and use it in GitHub Desktop.
Jquery Tokeinput & Dynamic Ruby on Rails Nested Form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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