# Changin var from partial

**URL:** <https://www.ruby-forum.com/t/changin-var-from-partial/163258>\
**Category:** Rails\
**Created:** [March 18, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/changin-var-from-partial/163258 "2009-03-18T18:18:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luciano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f05b48/32.png) [@Luciano](https://www.ruby-forum.com/u/Luciano)\
**Post date:** [March 18, 2009, 6:18pm UTC](https://www.ruby-forum.com/t/changin-var-from-partial/163258/1 "2009-03-18T18:18:31Z")

</div>

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

\<% @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

 \<% 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 %\>

\<% end %\>

* * *

Does anyone have any tip on it?

thanks

Luciano

---

<div class="post-metadata">

**Author:** ![Luciano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f05b48/32.png) [@Luciano](https://www.ruby-forum.com/u/Luciano)\
**Post date:** [March 18, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/changin-var-from-partial/163258/2 "2009-03-18T18:29:04Z")

</div>

On Mar 18, 5:14 pm, Luciano [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Luciano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f05b48/32.png) [@Luciano](https://www.ruby-forum.com/u/Luciano)\
**Post date:** [March 18, 2009, 7:15pm UTC](https://www.ruby-forum.com/t/changin-var-from-partial/163258/3 "2009-03-18T19:15:27Z")

</div>

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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Luciano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f05b48/32.png) [@Luciano](https://www.ruby-forum.com/u/Luciano)\
**Post date:** [March 18, 2009, 7:43pm UTC](https://www.ruby-forum.com/t/changin-var-from-partial/163258/4 "2009-03-18T19:43:42Z")

</div>

On Mar 18, 6:14 pm, Luciano [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![Luciano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f05b48/32.png) [@Luciano](https://www.ruby-forum.com/u/Luciano)\
**Post date:** [April 7, 2009, 12:17pm UTC](https://www.ruby-forum.com/t/changin-var-from-partial/163258/5 "2009-04-07T12:17:30Z")

</div>

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:

......\<%=

observe\_field :text\_1 %\>

......\<%=

observe\_field :text\_2 %\>

......\<%=

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

---

<div class="post-metadata">

**Author:** ![Luciano](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f05b48/32.png) [@Luciano](https://www.ruby-forum.com/u/Luciano)\
**Post date:** [March 18, 2009, 8:13pm UTC](https://www.ruby-forum.com/t/changin-var-from-partial/163258/6 "2009-03-18T20:13:03Z")

</div>

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 C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
