Called javascript method and ruby method with 1 button?


#1

Hi

I create a javascript method in rhtml with
and I would like to have 1 button that will call my controller method
and js() at the same time, so user will only need to click 1 button
instead of 2

Is this possible to do?

Thank you


#2

You can use the link_to_function helper to setup the call to JS or
just use straight js to do that. Inside your js function use
Ajax.Request or one of its subclasses to call the controller action
using XmlHttpRequest.

Michael

On Apr 22, 3:06 am, Lalala L. removed_email_address@domain.invalid


#3

You can use button_to_function with remote_function to create a button
that will use Ajax to call an action in your controller.

The method remote_function takes the same parameters as
link_to_remote, which means that you can then use parameters
like :before, :after, :complete, etc. to call arbitrary JavaScript at
various points during the Ajax request.

For example:

<%= button_to_function(“Click me”, remote_function(:url => {:action
=> :do_stuff}, :before => “myJavaScriptFunction()”)) %>

Chris

On Apr 22, 11:06 am, Lalala L. removed_email_address@domain.invalid