# Html form and form\_for

**URL:** <https://www.ruby-forum.com/t/html-form-and-form-for/122466>\
**Category:** Rails\
**Created:** [December 7, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/html-form-and-form-for/122466 "2007-12-07T20:43:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [December 7, 2007, 8:43pm UTC](https://www.ruby-forum.com/t/html-form-and-form-for/122466/1 "2007-12-07T20:43:23Z")

</div>

So, i’m not sure exactly how to do this, or if it is a smart thing to do  
this. I need some help on the best way to approach this.

I have a simple form, it has a text\_field and a text\_area. I want to add  
2 submit buttons. Each one will do a different action. I was thinking I  
could do form\_for, and fields\_for, but then I thought that fields\_for is  
used for having two objects, but I only have one. I know I could just  
make them images, and links, but I suck at photoshop, and my boss is one  
of those people that feels he is a designer, and he tells me that I have  
to have some sort of flashing animated gif that is on the right hand  
side, and some link that has to be red over on the left side. He also  
told me I have to have two buttons, but I don’t know how to do that.

\<% form\_for(:trouble\_ticket, @ticket, :url =\> { update\_url }) do |f| %\>

Repaired by:  
 \<%= f.text\_field(:repair\_tech\_name) %\>

Comments:  
 \<%= f.text\_area(:comments) %\>

\<%= submit\_tag('Close ticket') %\> or \<%= submit\_tag('Assign Ticket') %\>

\<% end %\>

Thanks,

~Jeremy

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [December 7, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/html-form-and-form-for/122466/2 "2007-12-07T22:51:08Z")

</div>

This is just me, but I’d make them both buttons and assign a  
javascript function to the onclick events. In the function, I’d grab  
the form via prototype, set the action accordingly, then submit it.  
There’s probably a more Rails-pure way of doing it, but I generally  
go with what works first.

Peace,  
Phillip

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [December 8, 2007, 12:03am UTC](https://www.ruby-forum.com/t/html-form-and-form-for/122466/3 "2007-12-08T00:03:33Z")

</div>

Phillip K. wrote:

> This is just me, but I’d make them both buttons and assign a  
> javascript function to the onclick events. In the function, I’d grab  
> the form via prototype, set the action accordingly, then submit it.  
> There’s probably a more Rails-pure way of doing it, but I generally  
> go with what works first.
> 
> Peace,  
> Phillip

that sounds awesome, unfortunately my javascript skills suck, but I will  
see what I can come up with doing it that way. Thanks for the advice.

~Jeremy

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [December 8, 2007, 1:28am UTC](https://www.ruby-forum.com/t/html-form-and-form-for/122466/4 "2007-12-08T01:28:32Z")

</div>

Jeremy W. wrote:

> Phillip K. wrote:
> 
> > This is just me, but I’d make them both buttons and assign a  
> > javascript function to the onclick events. In the function, I’d grab  
> > the form via prototype, set the action accordingly, then submit it.  
> > There’s probably a more Rails-pure way of doing it, but I generally  
> > go with what works first.
> > 
> > Peace,  
> > Phillip
> 
> that sounds awesome, unfortunately my javascript skills suck, but I will  
> see what I can come up with doing it that way. Thanks for the advice.
> 
> ~Jeremy

here is a simple javascript call from an onclick:

THIS IS A BUTTON IN THE VIEW RHTML FILE:

THE BUTTON NAME

PUT THIS IN THE HEADER SECTION OF THE RHTML

\<script type='text/javascript"\> function mycoolfuction(){ alert('I BAD. I BAD. YOU KNOW IT!'); }

now, for you easy javascript pleasure, go here,  
and you will love all this javascript all over the place:

> **[JavaScript Tutorial](https://www.w3schools.com/js/default.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

i did not get into server side junk, and i am not sure you need it.

---

<div class="post-metadata">

**Author:** ![nuby2ruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/54ee81/32.png) [@nuby2ruby](https://www.ruby-forum.com/u/nuby2ruby)\
**Post date:** [December 8, 2007, 1:47am UTC](https://www.ruby-forum.com/t/html-form-and-form-for/122466/5 "2007-12-08T01:47:53Z")

</div>

Hi Jeremy,

Sorry to state the obvious here, but there’s only one way for your JS  
skills to get better…code it. I’ve been writing JS for less than a  
year, and while I don’t have the knowledge or skills to work on  
Prototype or Scriptaculous (or any other wonderful library), I can  
look at it and understand it (for the most part). Heh, I remember  
the first little JS thing I did. I was “Oh! That’s so cool!” But you  
have to start somewhere.

For this purpose, your code is going to look something like:

function close\_ticket()  
{  
var f = $(‘ticket\_form’);  
f.action = ‘url to closing ticket’;  
f.submit();  
}

function assign\_ticket()  
{  
var f = $(‘ticket\_form’);  
f.action = ‘url to assigning ticket’;  
f.submit();  
}

Assuming you have no special logic for either closing or assigning,  
you can make this a single function and pass in some value that  
indicates what you want to do:

function submit\_form(action)  
{  
var = $(‘ticket\_form’);

if (action == ‘close’)  
{  
f.action = ‘url to close’;  
}  
else  
{  
f.action = ‘url to assign’;  
}

f.submit();  
}

then both buttons would call the same function:

I haven’t tried this code, so there might be some holes, but the  
theory is sound. There are also other ways you could change it here  
and there to make it more compact and dry. I’ll leave that to you.

Ahh…after taking from the community for so long, it’s good to  
finally be able to give something back.

Peace,  
Phillip
