Hello, I have a simple link_to_function to add a group of form fiels, to use observe_field I must have unique names... I created a var to store the itens count but the partial is not updating it''s value. **************************************************************************************************** _form.html.erb <div id="products"> </div> <% @count = 0 %> <%= add_product_link "New product" %> **************************************************************************************************** quotes_helper.rb module QuotesHelper def add_product_link(name) link_to_function name do |page| @atual = @atual + 1 page.insert_html :bottom, :products, :partial => "quote_product", :object => QuoteProduct.new end end end **************************************************************************************************** _quote_product.html.erb <div class="product"> <% fields_for "quote[product_attributes][]", quote_product do |f| %> <p> <%= @count %> <%= f.select :product_id, @products, {}, :index => nil, :prompt => "Escolha uma opção..." %> <%= f.text_field :quantity, :size => 12, :index => nil %> <%= f.select :colors, 1..4, {}, :index => nil %> <%= f.text_field :discount, :size => 12, :index => nil % > <%= @count = @count + 1 %> </p> <% end %> </div> **************************************************************************************************** Does anyone have any tip on it? thanks Luciano --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
On Mar 18, 5:14 pm, Luciano <bonach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > I have a simple link_to_function to add a group of form fiels, to use > observe_field I must have unique names... > > I created a var to store the itens count but the partial is not > updating it''s value. >I think there''s two things happening here. My understanding of the matter is that when a template is rendered it will get a copy of the controllers instance variables. Changes it makes to those instance won''t persist though (the next template rendered will get a fresh copy of those instance variables). However I think you''re actually falling down another whole: you''re expecting each click on the add product link to re-evaluate the template. This cannot happen: link_to_function produces entirely client side javascript: your _quote_product.html.erb is only rendered once. If you look at the html source of your page you''ll see than rendered html as a literal inside a blob of javascript. Fred --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Hi Fred, thanks for your advice! Do you believe link_to_remote would be the solution here? Luciano On Mar 18, 2:28 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 18, 5:14 pm, Luciano <bonach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > > I have a simple link_to_function to add a group of form fiels, to use > > observe_field I must have unique names... > > > I created a var to store the itens count but the partial is not > > updating it''s value. > > I think there''s two things happening here. My understanding of the > matter is that when a template is rendered it will get a copy of the > controllers instance variables. Changes it makes to those instance > won''t persist though (the next template rendered will get a fresh copy > of those instance variables). > > However I think you''re actually falling down another whole: you''re > expecting each click on the add product link to re-evaluate the > template. This cannot happen: link_to_function produces entirely > client side javascript: your _quote_product.html.erb is only rendered > once. If you look at the html source of your page you''ll see than > rendered html as a literal inside a blob of javascript. > > Fred--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
On Mar 18, 6:14 pm, Luciano <bonach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Fred, > > thanks for your advice! > > Do you believe link_to_remote would be the solution here? >link_to_remote is a possibility, although you''d have to pass some parameter in to let it know what id it should generate. This sort of thing can get quite messy - you might find the nicest way out would involve writing client side code that would gsub the generated html to stick in an appropriate id (since it''s easy for the client side code to test whether a dom element with a given id already exists) or to just always generate a unique id (eg based on the current time in milliseconds) Fred> Luciano > > On Mar 18, 2:28 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Mar 18, 5:14 pm, Luciano <bonach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > > > I have a simple link_to_function to add a group of form fiels, to use > > > observe_field I must have unique names... > > > > I created a var to store the itens count but the partial is not > > > updating it''s value. > > > I think there''s two things happening here. My understanding of the > > matter is that when a template is rendered it will get a copy of the > > controllers instance variables. Changes it makes to those instance > > won''t persist though (the next template rendered will get a fresh copy > > of those instance variables). > > > However I think you''re actually falling down another whole: you''re > > expecting each click on the add product link to re-evaluate the > > template. This cannot happen: link_to_function produces entirely > > client side javascript: your _quote_product.html.erb is only rendered > > once. If you look at the html source of your page you''ll see than > > rendered html as a literal inside a blob of javascript. > > > Fred--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Fred, this milliseconds idea is just great! I think I can make it work this way. thank your very much! Luciano On Mar 18, 3:43 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 18, 6:14 pm, Luciano <bonach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Fred, > > > thanks for your advice! > > > Do you believe link_to_remote would be the solution here? > > link_to_remote is a possibility, although you''d have to pass some > parameter in to let it know what id it should generate. This sort of > thing can get quite messy - you might find the nicest way out would > involve writing client side code that would gsub the generated html to > stick in an appropriate id (since it''s easy for the client side code > to test whether a dom element with a given id already exists) or to > just always generate a unique id (eg based on the current time in > milliseconds) > > Fred > > > Luciano > > > On Mar 18, 2:28 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > wrote: > > > > On Mar 18, 5:14 pm, Luciano <bonach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > > > > I have a simple link_to_function to add a group of form fiels, to use > > > > observe_field I must have unique names... > > > > > I created a var to store the itens count but the partial is not > > > > updating it''s value. > > > > I think there''s two things happening here. My understanding of the > > > matter is that when a template is rendered it will get a copy of the > > > controllers instance variables. Changes it makes to those instance > > > won''t persist though (the next template rendered will get a fresh copy > > > of those instance variables). > > > > However I think you''re actually falling down another whole: you''re > > > expecting each click on the add product link to re-evaluate the > > > template. This cannot happen: link_to_function produces entirely > > > client side javascript: your _quote_product.html.erb is only rendered > > > once. If you look at the html source of your page you''ll see than > > > rendered html as a literal inside a blob of javascript. > > > > Fred--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---
Hi, Did you manage to get this working? I''m having exactly the same issue whereby I need to observe a field within a dynamically inserted partial, e.g: <!-- Partial --><tr id="row_1">...<input id="text_1" />...</tr><%observe_field :text_1 %> <!-- Partial --><tr id="row_2">...<input id="text_2" />...</tr><%observe_field :text_2 %> <!-- Partial --><tr id="row_3">...<input id="text_3" />...</tr><%observe_field :text_3 %> I need to be able to generate the text_1, text_2, text_3, etc. id''s dynamically, preferably using javascript, and then observe them with the Rail''s observe_field helper. However, because the partial is only rendered once, each row ends up with the same id. Kind regards, Chris On Mar 18, 8:12 pm, Luciano <bonach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Fred, > > this milliseconds idea is just great! > > I think I can make it work this way. > > thank your very much! > > Luciano > > On Mar 18, 3:43 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > On Mar 18, 6:14 pm, Luciano <bonach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Fred, > > > > thanks for your advice! > > > > Do you believe link_to_remote would be the solution here? > > > link_to_remote is a possibility, although you''d have to pass some > > parameter in to let it know what id it should generate. This sort of > > thing can get quite messy - you might find the nicest way out would > > involve writing client side code that would gsub the generated html to > > stick in an appropriate id (since it''s easy for the client side code > > to test whether a dom element with a given id already exists) or to > > just always generate a unique id (eg based on the current time in > > milliseconds) > > > Fred > > > > Luciano > > > > On Mar 18, 2:28 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > > > wrote: > > > > > On Mar 18, 5:14 pm, Luciano <bonach...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, > > > > > > I have a simple link_to_function to add a group of form fiels, to use > > > > > observe_field I must have unique names... > > > > > > I created a var to store the itens count but the partial is not > > > > > updating it''s value. > > > > > I think there''s two things happening here. My understanding of the > > > > matter is that when a template is rendered it will get a copy of the > > > > controllers instance variables. Changes it makes to those instance > > > > won''t persist though (the next template rendered will get a fresh copy > > > > of those instance variables). > > > > > However I think you''re actually falling down another whole: you''re > > > > expecting each click on the add product link to re-evaluate the > > > > template. This cannot happen: link_to_function produces entirely > > > > client side javascript: your _quote_product.html.erb is only rendered > > > > once. If you look at the html source of your page you''ll see than > > > > rendered html as a literal inside a blob of javascript. > > > > > Fred > >--~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@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 -~----------~----~----~----~------~----~------~--~---