Getting form field value within link_to_function


#1

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


#2

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