Dynamic Form Clicking

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?

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

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

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?

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

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. :wink:

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