Button on view

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

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

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 :frowning:

Actually, button_to_remote doesn’t exist :frowning:

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

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

Ah, never thought of that :slight_smile:

Thanks