Hi people. I''m developing a rails 3.0.9 app, I''m using "accept nested attributes for" to add dynamically new "child" items to its parent. the thing is every child has the same id, and I need every child with its own id, because I need to make some jquery functions to work with them. Is there a way to achieve this? Tanks for your help -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
How did all the children end up with the same ID? Posting some of your problematic code can help us help you. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/2-vkrTp98doJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 4 January 2012 16:47, Angelo Cordova <acordinz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi people. > > I''m developing a rails 3.0.9 app, I''m using "accept nested attributes > for" to add dynamically new "child" items to its parent. the thing is > every child has the same id, and I need every child with its own id, > because I need to make some jquery functions to work with them.I presume you are talking about the id in the html rather than the id of the record in the database. When you generate the html for the child, just allocate a new id for each. Since you have not shown us how you generate the html it is difficult to comment. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Angelo Cordova
2012-Jan-04 19:38 UTC
Re: How to set different id in nested form attributes
On 4 ene, 13:58, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 4 January 2012 16:47, Angelo Cordova <acord...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi people. > > > I''m developing a rails 3.0.9 app, I''m using "accept nested attributes > > for" to add dynamically new "child" items to its parent. the thing is > > every child has the same id, and I need every child with its own id, > > because I need to make some jquery functions to work with them. > > I presume you are talking about the id in the html rather than the id > of the record in the database. When you generate the html for the > child, just allocate a new id for each. Since you have not shown us > how you generate the html it is difficult to comment. > > ColinYes, I''m not saying that the DB id is the same, I''m saying the html attribute "id" is the same, sorry for that. But I don''t know how to set a different id for each child. I''m using nested attributes in this way, to simplify I will use "document" and "products", where a "document" has many "products": app/models/document class Document < ActiveRecord::Base has_many :details,:class_name => ''Product'', :foreign_key => ''document_id'', :dependent => :destroy accepts_nested_attributes_for :details, :allow_destroy => :true end app/models/product class Product < ActiveRecord::Base belongs_to :document :foreign_key => ''document_id'' end app/controllers/documents class DocumentsController < ApplicationController def index... def show... def new @document = Document.new @document.products.build respond_to do |format| format.html # new.html.erb format.xml { render :xml => @document } end end def... ... .. end app/views/documents/_form <%= form_for @document do |f| %> (document fields such as number, provider, date, etc) <h3>Products</h3> <div class="details"> <% if @document.id %> <%= f.fields_for :details do |d| %> <%= render "details", :f => d %> <% end %> <% end %> <%= link_to_add_fields "+ Add Product", f, :details %> </div> ... ... (button for "submit") <% end %> app/views/documents/_details <div class="fields"> <%= f.label :product %> <%= f.collection_select(:document_id, Document.all, :id, :number, options = {:prompt => ''''},:class => ''_prod select sbig'') %> <%= link_to_remove_fields "remove product", f %> </div> app/helpers/applicationhelper 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, :f => builder) end link_to_function(name, ("add_fields(this, ''#{association}'', ''#{escape_javascript(fields)}'')")) end end public/javascripts/application.js 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)); } So, in _details, I didn''t set an id (html attribute) for product but If I check with firebug, The id is the same for each "detail", if I try to set manually, obviously, each "detail" get the same id. Please, help me -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Angelo Cordova
2012-Jan-04 21:14 UTC
Re: How to set different id in nested form attributes
I already solve this I''m really sorry, I''ve just realized that every child had its own "id", so it was easier to get it solved. Thanks for your help and for sharing your time -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.