Forum: Ruby on Rails Getting form field value within link_to_function

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Luca R. (Guest)
on 2009-05-14 15:50
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 =>
 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 =>
  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
Luca R. (Guest)
on 2009-05-14 17:44
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 =>
    page << "}"
  end %>
Posting it as maybe someone else will need it.
This topic is locked and can not be replied to.