# Dynamically generate fields in view

**URL:** <https://www.ruby-forum.com/t/dynamically-generate-fields-in-view/160659>\
**Category:** Rails\
**Created:** [February 18, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/dynamically-generate-fields-in-view/160659 "2009-02-18T15:44:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mpathare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@mpathare](https://www.ruby-forum.com/u/mpathare)\
**Post date:** [February 18, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/dynamically-generate-fields-in-view/160659/1 "2009-02-18T15:44:19Z")

</div>

Hello,  
what would be the best way to solve this?

I have a view where I want users to be able to enter the score of a  
tennis match. The number of sets to be played is determined from a  
column in a database table. Each sports club, when set up, can set the  
total number of sets to be played in a match…5, 3, 1…whatever.

This number will determine how many text boxes are displayed for the  
user to enter the score. The match model has only one column for score,  
so before the score is saved in the database it must be created as  
follows

final\_score = set\_1 + set\_2 + set\_3 (for a match in which 3 sets must be  
played)

I think maybe using virtual attributes is the way to go, but I am not  
sure how to implement it.

Thanks.

---

<div class="post-metadata">

**Author:** ![mpathare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@mpathare](https://www.ruby-forum.com/u/mpathare)\
**Post date:** [February 18, 2009, 4:08pm UTC](https://www.ruby-forum.com/t/dynamically-generate-fields-in-view/160659/2 "2009-02-18T16:08:34Z")

</div>

if you name each of your (game)set input fields something like “set[]”  
then rails will parse the parameters as an array. This way you will  
give you a params hash like:

{ ‘match’ =\> { ‘sets’ =\> [‘1’,‘3’,‘7’]} }

This means in your model you can define a virtual attribute called  
‘sets’ like:

def sets= scores  
self.score = scores.map(&:to\_i).sum  
end

On Feb 18, 3:44 pm, Maulin pa [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mpathare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@mpathare](https://www.ruby-forum.com/u/mpathare)\
**Post date:** [February 18, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/dynamically-generate-fields-in-view/160659/3 "2009-02-18T17:22:22Z")

</div>

> def sets= scores  
> self.score = scores.map(&:to\_i).sum  
> end

Thanks! I will try that. Also, would it be possible to add validations  
in the model for each of the sets?

---

<div class="post-metadata">

**Author:** ![mpathare](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/9fc29f/32.png) [@mpathare](https://www.ruby-forum.com/u/mpathare)\
**Post date:** [February 18, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/dynamically-generate-fields-in-view/160659/4 "2009-02-18T17:38:13Z")

</div>

> Thanks! I will try that. Also, would it be possible to add validations  
> in the model for each of the sets?

I don’t think you can use rails’ standard ‘valtidates’ methods, like  
validates\_presence\_of, but you can add your own custom validate method  
to your model:

def validate  
self.errors.add(:sets, ‘are invalid’) unless   
end
