# Button on view

**URL:** <https://www.ruby-forum.com/t/button-on-view/65528>\
**Category:** Rails\
**Created:** [August 18, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/button-on-view/65528 "2006-08-18T21:39:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 18, 2006, 9:39pm UTC](https://www.ruby-forum.com/t/button-on-view/65528/1 "2006-08-18T21:39:07Z")

</div>

Hi,

I have a page containing 3 views (2 are partials). One of the partials  
needs to have a button that will clear a table called “messages” in the  
database.

I can put a button like this “\<%= button\_to “Clear Messages”, :action =\>  
‘clear\_messages’ %\>” and have the controller do “Message.delete\_all”.  
But the problem is that the controller would want to render a view, and  
I don’t want that to happen. I just want a particular action to occur in  
the backend without any changes to the page.

Actually I would need such buttons for other purposes too, but if I know  
how to do the above example I could do the rest.

Any help would be greatly appreciated. Thanks,

Aditya

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 18, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/button-on-view/65528/2 "2006-08-18T21:45:21Z")

</div>

def clear\_messages  
Message.delete\_all  
render :nothing =\> true  
end

You also will need to make you button\_to a button\_to\_remote for an ajax  
call

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 18, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/button-on-view/65528/3 "2006-08-18T21:52:25Z")

</div>

Thanks for the reply Mark. Unfortunately, I had tried your solution  
already. The problems are:

> render :nothing =\> true

This renders a blank page. It would not do that if the call were Ajax,  
but:

> You also will need to make you button\_to a button\_to\_remote for an ajax  
> call

Actually, button\_to\_remote doesn’t exist ☹

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 18, 2006, 10:00pm UTC](https://www.ruby-forum.com/t/button-on-view/65528/4 "2006-08-18T22:00:23Z")

</div>

> Actually, button\_to\_remote doesn’t exist ☹

true! You can use a link\_to\_remote also, or just do a normal button and  
add  
you own ajax call in the onclick.

---

<div class="post-metadata">

**Author:** ![ar1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d9b06d/32.png) [@ar1](https://www.ruby-forum.com/u/ar1)\
**Post date:** [August 18, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/button-on-view/65528/5 "2006-08-18T23:23:23Z")

</div>

> just do a normal button and add you own ajax call in the onclick.

Ah, never thought of that 🙂

Thanks
