Reboot Reboot - 6 months ago 8
jQuery Question

Jquery running code before pressing the button

I am not sure if this is the correct way to do this or not. In my Rails application I have something like this.

<input id='foo' type="submit" value=" - Test - "/>

<script>
$(document).ready(function () {

$("#foo").click(function() {
alert("Clicked Test button");
'<% j @customer.do_something('Test', 'test') %>'; <-- This line is running without user pressing the button.
alert("Done");
});

</script>


Is it possible to call some method on
@customer
object when the button is pressed? Or do I need to do this via ajax? Currently
@customer.do_something
is executed even when I haven't pressed the button.

What is the correct way of calling Model methods once the button has been pressed?

Answer

The ERB (ie. the stuff in <% %>s) will be run on the server BEFORE the content is sent to the browser.

If you want to run it as the result of the click then yes, you will need to use Ajax.

Comments