# Is it possible to use link\_to\_function to increment value onclick?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-use-link-to-function-to-increment-value-onclick/181084>\
**Category:** Rails\
**Created:** [January 5, 2010, 6:39am UTC](https://www.ruby-forum.com/t/is-it-possible-to-use-link-to-function-to-increment-value-onclick/181084 "2010-01-05T06:39:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [January 5, 2010, 6:39am UTC](https://www.ruby-forum.com/t/is-it-possible-to-use-link-to-function-to-increment-value-onclick/181084/1 "2010-01-05T06:39:16Z")

</div>

Currently, I allow my users to dynamically add new fields to a form  
using the link\_to\_function variable. However, whenever a new field is  
added, its inputs have the same names as the previous fields. I  
solved this by passing in a local variable, but I can’t get that value  
to increment (or update) on click.

I’m using the following code:

\<%= link\_to\_function “Add Another” do |page|  
page.insert\_html :bottom, :tasks, :partial =\> ‘stuff’, :object =\>  
Model.new, :locals =\> { :num =\> 1 }  
end%\>

(Yes, that code was take from ryan bates’s complex forms series.)

How do I get that :num =\> 1 to increment every time a user clicks “Add  
Another.” increment and increment! don’t seem to work, and I thought  
about using Time.now.to\_i instead of single digit integers (note: it  
doesn’t matter what “num” is as long as it’s a number), but that only  
loads the time when page is first loaded, and doesn’t update when “Add  
Another” is clicked.

So, is there anyway to increment this :num value using client side  
javascript?

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [January 5, 2010, 9:07am UTC](https://www.ruby-forum.com/t/is-it-possible-to-use-link-to-function-to-increment-value-onclick/181084/2 "2010-01-05T09:07:21Z")

</div>

There are 2 solutions for this.

1. If you have problem with same name then use array for field name.

2. If you want to update num variable value then you have to update  
link itself.

I prefer 1st option because of it become easy to get values of unknown  
fields (because you don’t know how many fields are there).

I hope you get my point. or let me know…

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [January 5, 2010, 9:47am UTC](https://www.ruby-forum.com/t/is-it-possible-to-use-link-to-function-to-increment-value-onclick/181084/3 "2010-01-05T09:47:44Z")

</div>

hmm… I figured it would be easy. then I played with it. It is not  
easy. I was able to get it working in pure javascript, but could not  
get it working in rails… maybe someone else can get it, but it  
seemed to me rails was loading the partial as part of the link AKA the  
local is set when the link is created.

if you want to see it working in JavaScript it is not too hard. The  
Main pain is no partial use, you gotta build the partial in the  
link…

`

Add Another
`

I am sure there are a million ways to do it, and some may be better,  
but I am pretty sure link\_to\_function will not be in the answer to  
many if any of them.

Chris  
CowboyonRails

On Jan 4, 9:37 pm, AlwaysCharging [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Model.new, :locals =\> { :num =\> 1 }
> 
> So, is there anyway to increment this :num value using client side  
> javascript?

–

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 removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [January 5, 2010, 6:30pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-use-link-to-function-to-increment-value-onclick/181084/4 "2010-01-05T18:30:29Z")

</div>

On Jan 5, 3:46Â am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> `
> "'>" }); return false;">Add Another
> On Jan 4, 9:37Â pm, AlwaysCharging wrote:
> 
> doesn’t matter what “num” is as long as it’s a number), but that only
> loads the time when page is first loaded, and doesn’t update when “Add
> Another” is clicked.
> 
> So, is there anyway to increment this :num value using client side
> javascript?
> 
> `

`
Why do you want to use javascript to increment the field count? This
seems like it’d be so much easier to just store the field count in the
session and be done with it.
An example for a webmail app I hacked out awhile ago (though I’m still
not sure this counting approach in general is the best way to do
things:)
Relevant section of main form (Haml):
%fieldset#attachments
%a#add_attachment{ :href => add_attachment_path } Add Attachment

attachment partial:
%p.attachment
=file_field :email, “attachments[#{session[‘attachment_count’]}]”
%a.remove{ :href => “#” } Remove

javascript:
$(“#add_attachment”).click(function() {
$.get($(this).attr(“href”), function(page) {
$(“#attachments”).append(page);
});
return false;
});

add_attachment action:
def add_attachment
session[‘attachment_count’] += 1
render :partial => ‘attachment’, :layout => false
end`

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [January 5, 2010, 11:59pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-use-link-to-function-to-increment-value-onclick/181084/5 "2010-01-05T23:59:25Z")

</div>

Your pure javascript way is great!, and a lot cleaner than the  
link\_to\_function, which I don’t really like anyway. Is there a way to  
pass in the partial with your javascript method? Like instead of  
“input type=‘text’ name='random\_input\_&quot”, how can I pass in  
“:partial =\> ‘stuff’, :object =\> Feed.new” and use getCount() as  
the :num?

Sorry, I really am noob when it comes to raw Javascript. I’ve been  
playing around with it and can’t get it to work. I do think this way  
will be perfect though, so thank you for your help, much appreciated.

On Jan 5, 3:46Â am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![AlwaysCharging](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/ee7513/32.png) [@AlwaysCharging](https://www.ruby-forum.com/u/AlwaysCharging)\
**Post date:** [January 5, 2010, 11:52pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-use-link-to-function-to-increment-value-onclick/181084/6 "2010-01-05T23:52:23Z")

</div>

I’m using each\_with\_index, but can’t the index value to update each  
time the user “Adds Another”, which is why I was defining a new  
variable and passing it as well. So I guess that’s my real problem,  
getting each\_with\_index to update.
