# :onclick function executing on page load

**URL:** <https://www.ruby-forum.com/t/onclick-function-executing-on-page-load/114816>\
**Category:** Rails\
**Created:** [September 17, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/onclick-function-executing-on-page-load/114816 "2007-09-17T19:58:08Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 17, 2007, 7:58pm UTC](https://www.ruby-forum.com/t/onclick-function-executing-on-page-load/114816/1 "2007-09-17T19:58:08Z")

</div>

The following code in my rhtml file creates an ‘event’ object when the  
‘accept agreement’ link is clicked, but also creates an ‘event’ object  
when the page loads.

\<%= submit\_tag ‘Accept Agreement’, :onclick =\> Event.create(:object\_id  
=\> 1, :name =\> ‘agreement’, :date =\> Time.now).save! %\>

How might I change my code so that an ‘event’ is created only upon  
clicking?

Thanks,

Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 17, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/onclick-function-executing-on-page-load/114816/2 "2007-09-17T23:29:04Z")

</div>

On 9/17/07, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The following code in my rhtml file creates an ‘event’ object when the  
> ‘accept agreement’ link is clicked, but also creates an ‘event’ object  
> when the page loads.
> 
> \<%= submit\_tag ‘Accept Agreement’, :onclick =\> Event.create(:object\_id  
> =\> 1, :name =\> ‘agreement’, :date =\> Time.now).save! %\>

Erm, yeah, that’s messed up.

Event.create is Ruby code (I’m assuming Event is a model in your  
application), and it’s being executed as the template is being  
rendered. The return from save! (which is redundant, because create  
saves the row) is passed as the content for the onclick attribute of  
the generated submit button.

Rather than trying to “fix” this, I think you need to back up and walk  
through a Rails tutorial so you can get the basic pattern down. For  
instance, you will rarely (if ever) use an onclick event on a submit  
button.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [September 17, 2007, 11:34pm UTC](https://www.ruby-forum.com/t/onclick-function-executing-on-page-load/114816/3 "2007-09-17T23:34:24Z")

</div>

Bob S. wrote:

> Erm, yeah, that’s messed up.
> 
> Event.create is Ruby code (I’m assuming Event is a model in your  
> application), and it’s being executed as the template is being  
> rendered. The return from save! (which is redundant, because create  
> saves the row) is passed as the content for the onclick attribute of  
> the generated submit button.
> 
> Rather than trying to “fix” this, I think you need to back up and walk  
> through a Rails tutorial so you can get the basic pattern down. For  
> instance, you will rarely (if ever) use an onclick event on a submit  
> button.

Yeah, I somehow thought I could get away with putting that in the view  
code. I’ll just create the ‘event’ in the controller though  
form\_remote\_tag. Thanks for your feedback!

Peter
