# Getting form field value within link\_to\_function

**URL:** <https://www.ruby-forum.com/t/getting-form-field-value-within-link-to-function/167959>\
**Category:** Rails\
**Created:** [May 14, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/getting-form-field-value-within-link-to-function/167959 "2009-05-14T13:50:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [May 14, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/getting-form-field-value-within-link-to-function/167959/1 "2009-05-14T13:50:13Z")

</div>

Hi all,  
I’m very new to this community, and I started practicing with Ruby on  
Rails just recently.  
I watched the 3 episodes , 72-73-74 for complex forms on railscasts ,  
and I’m trying to deploy a “new” function for my form based on what I  
saw in the 3 episodes.  
So, I have a form for my master table, with 2 fields: one is named  
“from\_pl” and the other is named “to\_pl”.  
What I’d like to achieve is the possibility of adding (to\_pl - from\_pl)  
children to the form with the click on a link.  
So my model is perfectly shaped, and the link Add child , in the vein of  
the referenced episode of railscasts is perfectly working, the code in  
the new.html.erb is  
\<%= link\_to\_function “Add Child” do |page|  
page.insert\_html :bottom, :percentages, :partial =\> ‘percentage’,  
:object =\> MatchPercentage.new  
end %\>  
I’d like to add more than 1 child , and I tried with a loop.  
So far, using  
\<%= link\_to\_function “Add Child” do |page|  
3.upto(6) do  
page.insert\_html :bottom, :percentages, :partial =\> ‘percentage’,  
:object =\> MatchPercentage.new  
end  
end %\>  
works fine. I’d like to substitute the 3 and the 6 with the values of  
the 2 fields in the form, which has not yet been submitted.  
I tried with a lot of different searches on the web, but didn’t find  
anything suitable.  
Could you please enlighten me if there is a solution or not?  
Thanks a lot, Kind regards

---

<div class="post-metadata">

**Author:** ![stratboy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/838e76/32.png) [@stratboy](https://www.ruby-forum.com/u/stratboy)\
**Post date:** [May 14, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/getting-form-field-value-within-link-to-function/167959/2 "2009-05-14T15:44:42Z")

</div>

OK guys,  
think I solved it by spending a little more time.  
Instead of using Ruby code for looping, I used js enflation with the  
page \<\< command.  
This is what did it for me:  
\<%= link\_to\_function “Add Child” do |page|  
page \<\< “for(i=$(‘from\_pl’).value;i\<=$(‘to\_pl’).value;i++) {”  
page.insert\_html :bottom, :percentages, :partial =\> ‘percentage’,  
:object =\> MatchPercentage.new  
page \<\< “}”  
end %\>  
Posting it as maybe someone else will need it.  
Cheers
