# Dynamic Form Clicking

**URL:** <https://www.ruby-forum.com/t/dynamic-form-clicking/134756>\
**Category:** Rails\
**Created:** [April 14, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/dynamic-form-clicking/134756 "2008-04-14T21:22:51Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 14, 2008, 9:22pm UTC](https://www.ruby-forum.com/t/dynamic-form-clicking/134756/1 "2008-04-14T21:22:51Z")

</div>

Is it possible to change the value of what is being submitted to a  
form based on the number of times it has been clicked, maybe using  
AJAX?

Let’s say I have a form of 3 users. Each user only has two actions:  
hit or miss a basket. Because there are ten baskets and each gets  
taken away as the user hits them, the last few baskets are worth more  
points.

In my form (a scorecard) I want the user to only have to click HIT  
(not select hit 1 or hit 2 from a select menu) should the user hit a  
basket. Then as they continue to hit HIT 9 more times, the points get  
higher.

How would I represent this in my view and controller?

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 15, 2008, 12:20am UTC](https://www.ruby-forum.com/t/dynamic-form-clicking/134756/2 "2008-04-15T00:20:41Z")

</div>

On Apr 14, 8:22 pm, edberner [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (not select hit 1 or hit 2 from a select menu) should the user hit a  
> basket. Then as they continue to hit HIT 9 more times, the points get  
> higher.

Well you’ve got 2 options really. You could have some client side  
stuff that handles all this, but really I wouldn’t. just have the form  
submit ‘hit’ and do all the clever working out of which hits are worth  
more points server side (and probably in your model rather than in the  
controller). Exactly how that pans out probably depends on your  
scoring algorithm.

Fred

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 15, 2008, 12:31am UTC](https://www.ruby-forum.com/t/dynamic-form-clicking/134756/3 "2008-04-15T00:31:48Z")

</div>

On Apr 14, 11:26 pm, Ellis B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What clever things are these? I’m still not quite sure how I would  
> implement this…  
> My algorithm is not very complex, it’s just 7, 8, 9 is worth 2 and 10  
> is worth 3

The controller/view should know nothing about this. I’m assuming  
somewhere you’ve got an object that represents the scorecord. That  
object should have an add hit method that tracks all of this.

Fred

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 15, 2008, 12:27am UTC](https://www.ruby-forum.com/t/dynamic-form-clicking/134756/4 "2008-04-15T00:27:07Z")

</div>

What clever things are these? I’m still not quite sure how I would  
implement this…  
My algorithm is not very complex, it’s just 7, 8, 9 is worth 2 and 10  
is worth 3

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 15, 2008, 12:39am UTC](https://www.ruby-forum.com/t/dynamic-form-clicking/134756/5 "2008-04-15T00:39:26Z")

</div>

I do however, I think I may have implemented it correctly.  
Right now it’s called action.  
Action Model  
string :action (what the select menu alters)  
player\_id (to go with each player)

each round has 6 players taking a shot, so the actions add up pretty  
quickly.  
this is why I’m looking for a better way to do this.

How could I make the scorecard model better?

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 15, 2008, 8:19am UTC](https://www.ruby-forum.com/t/dynamic-form-clicking/134756/6 "2008-04-15T08:19:13Z")

</div>

Does anybody have any ideas for taking this out of the controller(/  
view) and putting the functionality in the model.

---

<div class="post-metadata">

**Author:** ![edberner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f9ae1b/32.png) [@edberner](https://www.ruby-forum.com/u/edberner)\
**Post date:** [April 15, 2008, 8:47am UTC](https://www.ruby-forum.com/t/dynamic-form-clicking/134756/7 "2008-04-15T08:47:18Z")

</div>

Hi.

It’d be something like

(in your model class definition)

def add\_hit  
self.update\_attribute(“hits”, (hits || 0) + 1)  
end

Then on your object (in your controller):

def add\_hit  
@character = Character.find(params[:id])  
@ character.add\_hit  
end

then in your view you’d have:

\<%= link\_to(“add hit”, :action =\> ‘add\_hit’, :id =\> @character.id) %\>

Something like that.

Julian.

Hope this isn’t too basic for you. 😉

Learn Ruby on Rails! Check out the FREE VIDS (for a limited time)  
VIDEO #4 coming soon!  
[http://sensei.zenunit.com/](http://sensei.zenunit.com/)
