# State changing click on divs

**URL:** <https://www.ruby-forum.com/t/state-changing-click-on-divs/123292>\
**Category:** Rails\
**Created:** [December 16, 2007, 1:24am UTC](https://www.ruby-forum.com/t/state-changing-click-on-divs/123292 "2007-12-16T01:24:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 16, 2007, 1:24am UTC](https://www.ruby-forum.com/t/state-changing-click-on-divs/123292/1 "2007-12-16T01:24:54Z")

</div>

I’ve moving on from the data collection part of my app to the data  
manipulation part and I’m at loss how to proceed, whether I need to put  
asside my rails booka and learn some java or not.

I will have a view that iterates through a collection of objects then in  
turn increments another variable to create a series of div elements with  
content related to the object and increment. For example Objects [A…E]  
, Increments [1…3] yieldsing 15 div’s A1, A2, A3, B1, B2, B3, C1 …

In those divs I will be diplaying images and data based on data in the  
object attibutes and the interator.

I would like the entire

 clickable, not a button within the div. I  
want to call a controller method and pass at least the object\_id (of  
A-E) and the iterator (1-3 in this example). If a button were  
acceptable, I think i could have a form with two hidden fields passing  
the necessary information in params.

As clicking will call a method which will make changes to the database a  
form with a true post might have some advantages(from what i fuzzily  
understand)).

I am sorry if this question is very basic… and perhaps not even a  
reils question…although I am interested in the rails best practices  
solution if there are many solutions.

Do I build a div with some sort of " onclick " that can push an  
“invisible??” button within the div containing hidden fields?

Is there a rails helper that will wrap a div to make it clickable and  
which I can load with a couple parameters and will that be safe if the  
method called will make changes to my database?

in plain english, if I were in the second div A2.  
If anywhere in this div is clickced, call method change\_user\_records  
with letter\_id = A.id , number = 2

I know that isn’t anywhere close to code but it explains the simple  
thing I hope to do and don’t know which tool to turn to.

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 16, 2007, 1:54am UTC](https://www.ruby-forum.com/t/state-changing-click-on-divs/123292/2 "2007-12-16T01:54:38Z")

</div>

On Dec 16, 12:24 am, Tom N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> method called will make changes to my database?

If you want to do this with ajax, use remote\_function(). If not you  
could mess with link\_to. This has an option which will cause it to  
generate a link with an onclick which will dynamically create a form  
with the appropriate fields and submit it. I’m sure you can work out  
from the source for link\_to how to do this yourself

You could also create the forms, but have no form submit buttons. In  
which case your onclicks are just $(‘form\_id’).submit()

Fred.

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [December 17, 2007, 5:53am UTC](https://www.ruby-forum.com/t/state-changing-click-on-divs/123292/3 "2007-12-17T05:53:24Z")

</div>

> You could also create the forms, but have no form submit buttons. In  
> which case your onclicks are just $(‘form\_id’).submit()
> 
> Fred.

Thanks for the advice yesterday… I finally got it using method number  
three!

Hoorah! I’ll post the example for other struggling newbies in a day or  
so.(my coding machine is offline). There were a couple gotchas with  
setting the id’s for the form.

Now I need to start figuring out the default mouse tips, hover diplays  
and div css stuff to give users a clue that they can actually click on  
the given divs!

I also wonder what percentage of internet users the “onclick” stuff  
works for…  
; )
