I have a bit of code that formats an editing page depending on how many values a question has.
<% (1..@question.qvalues_count.to_i).each do |qvalue| %>
<% case qvalue
when 1
answer_value = @answer.value1
when 2
answer_value = @answer.value2
when 3
answer_value = @answer.value3
when 4
answer_value = @answer.value4
when 5
answer_value = @answer.value5
end %>
<label for="answer[value<%=qvalue%>]" class="fieldLabel" >Value<%=qvalue%></label>
<input type="text" id="answer[value<%=qvalue%>]" name="answer[value<%=qvalue%>]"
value="<%=answer_value%>"
/>
<% end %>
That looks very messy and not extensible. How can I replace the case statement with something that does not require me have a case for each value1..n class variable?
I have success with the old stand by, dynamic code, ie: the eval.
or more correctly, as pointed out by Abe
for some reason unknown, this would not work.