Hi,
My application allows players to enter a tennis match into a database.
Each match has at least one and a maximum of five sets, represented by
the set_scores table and model.
class Match < ActiveRecord::Base
has_many :set_scores
end
class SetScore < ActiveRecord::Base
end
DDL of set_scores
CREATE TABLE "public"."set_scores" (
"match_id" INTEGER NOT NULL,
"set_nr" SMALLINT NOT NULL,
"score_opp1" SMALLINT NOT NULL,
"score_opp2" SMALLINT NOT NULL,
CONSTRAINT "set_scores_fk" FOREIGN KEY ("match_id")
REFERENCES "public"."matches"("id")
ON DELETE CASCADE
ON UPDATE CASCADE
DEFERRABLE
INITIALLY DEFERRED
) WITH OIDS;
Now my problem is how do I generate the correct form for this?
(fragment _form.rhtml)
<% for i in 1..5 %>
<%= hidden_field "set_score[]", "set_nr", {:value => i
} %>
<tr>
<th>Set <%=i%></td>
<td class="center"><%= text_field "set_score[]",
''score_opp1'', "size"
=> 3 %></td>
<td class="center">-</td>
<td class="center"><%= text_field "set_score[]",
''score_opp2'', "size"
=> 3 %></td>
</tr>
<% end %>
This doesn''t work as it doesn''t create an array of set_scores,
the HTML
output is simply this:
<input id="set_score_set_nr" name="set_score[set_nr]"
type="hidden"
value="1" />
<tr>
<th>Set 1</td>
<td class="center"><input id="set_score_score_opp1"
name="set_score[score_opp1]" size="3" type="text"
/></td>
<td class="center">-</td>
<td class="center"><input id="set_score_score_opp2"
name="set_score[score_opp2]" size="3" type="text"
/></td>
</tr>
<input id="set_score_set_nr" name="set_score[set_nr]"
type="hidden"
value="2" />
<tr>
<th>Set 2</td>
<td class="center"><input id="set_score_score_opp1"
name="set_score[score_opp1]" size="3" type="text"
/></td>
<td class="center">-</td>
<td class="center"><input id="set_score_score_opp2"
name="set_score[score_opp2]" size="3" type="text"
/></td>
</tr>
...
I want it to output something like:
<input name="set_score[][set_nr]" type="hidden"
value="2" />
I also tried putting this in the controller
def new
@match = Match.new
@set_scores = [SetScore.new, SetScore.new, SetScore.new,
SetScore.new, SetScore.new]
end
And this in _form.rhtml
<% for @set_score in @set_scores %>
<tr>
<th>Set </td>
<td class="center"><%= text_field "set_score[]",
''score_opp1'', "size"
=> 3 %></td>
<td class="center">-</td>
<td class="center"><%= text_field "set_score[]",
''score_opp2'', "size"
=> 3 %></td>
</tr>
<% end %>
Again with no luck...
Jeroen