# Adding a button to a form

**URL:** <https://www.ruby-forum.com/t/adding-a-button-to-a-form/69026>\
**Category:** Rails\
**Created:** [September 18, 2006, 7:33am UTC](https://www.ruby-forum.com/t/adding-a-button-to-a-form/69026 "2006-09-18T07:33:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [September 18, 2006, 7:33am UTC](https://www.ruby-forum.com/t/adding-a-button-to-a-form/69026/1 "2006-09-18T07:33:07Z")

</div>

I’ve looked at the API, and the only ‘button’ reference I found was for  
submit. How would I add another button to a form? I tried:

button\_tag ‘Do Something’, :action =\> ‘do\_something’

But that gave me an error.

Any help would be appreciated.

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [September 18, 2006, 8:15am UTC](https://www.ruby-forum.com/t/adding-a-button-to-a-form/69026/2 "2006-09-18T08:15:07Z")

</div>

As far as I know, the form can only submit to one place, but you can  
put logic in the action it submits to that determines which button was  
clicked. I have a form that does this and the logic basically goes like  
this…

case params[“commit”]  
when “Do This”  
some code to do something  
when “Do That”  
some code to do something else  
end

The “commit” might be able to change to :commit. (Can’t remember.) The  
“Do This” and “Do That” are the values of the submit buttons.

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [September 18, 2006, 8:51am UTC](https://www.ruby-forum.com/t/adding-a-button-to-a-form/69026/3 "2006-09-18T08:51:10Z")

</div>

It’s simply input :type =\> ‘button’

John W Higgins  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [September 19, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/adding-a-button-to-a-form/69026/4 "2006-09-19T18:04:25Z")

</div>

John,  
It sounds so simple, yet I can’t seem to get it right. When I enter  
this  
line into my view:

```
   <%= input :type => 'button' %>

```

I get this error:

```
wrong number of arguments (1 for 2)

```

So, I changed it to :

```
 <%= input 'Do Something', :type => 'button' %>

```

which returns a different error:

`@Do Something’ is not allowed as an instance variable name

What am I missing, here.  
-Larry

On 9/17/06, John H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > submit. How would I add another button to a form? I tried:  
> > “Work, work, work…there is no satisfactory alternative.”  
> > — E.Taft Benson
> 
> >

–  
Best Regards,  
-Larry  
“Work, work, work…there is no satisfactory alternative.”  
— E.Taft Benson

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [September 19, 2006, 7:58pm UTC](https://www.ruby-forum.com/t/adding-a-button-to-a-form/69026/5 "2006-09-19T19:58:46Z")

</div>

Michael C. wrote:

> > I get this error:  
> > `@Do Something’ is not allowed as an instance variable name
> > 
> > > John W Higgins
> > > 
> > > >
> > 
> > “Work, work, work…there is no satisfactory alternative.”  
> > — E.Taft Benson
> > 
> > >
> 
> –  
> More software projects have gone awry for lack of calendar time than  
> for all other causes combined. – Fred Brooks

A neat trick for this that I picked up somewhere…

give your custom button a :name attribute.

\<%= submit ‘cancel’, :name=\>:cancel %\>

Then on submission, look for params[:cancel]. If it’s there, that  
button was pressed.

\_Kevin

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [May 4, 2007, 6:28am UTC](https://www.ruby-forum.com/t/adding-a-button-to-a-form/69026/6 "2007-05-04T06:28:11Z")

</div>

Try this:  
\<%= submit\_tag “Name”, :type =\> ‘button’ %\>

---

<div class="post-metadata">

**Author:** ![ldk2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/d9b06d/32.png) [@ldk2005](https://www.ruby-forum.com/u/ldk2005)\
**Post date:** [September 19, 2006, 6:38pm UTC](https://www.ruby-forum.com/t/adding-a-button-to-a-form/69026/7 "2006-09-19T18:38:29Z")

</div>

[api.rubyonrails.com](http://api.rubyonrails.com) has an entry for the input tag.

On 9/19/06, Larry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> -Larry
> 
> > >
> 
> –  
> Best Regards,  
> -Larry  
> “Work, work, work…there is no satisfactory alternative.”  
> — E.Taft Benson
> 
> >

–  
More software projects have gone awry for lack of calendar time than  
for all other causes combined. – Fred Brooks
