I have a form in my application, which has a checkbox. On submitting the form, i need to see if the checkbox is checked, and if it is – i need to modify an attribute of a model. Here is my code which does not work :
$("#customer_submit").click(function(){
if ($("#default_check").is(':checked')){
<%current_merchant.has_default = true
<%current_merchant.save%>
}
});
The Ruby code gets implemented even if the check is not satisfied. I know that i am doing something fundamentally wrong.Please help!
You are mixing client side scripting with server side code.
The ruby code inside the <% %> is being executed before the code is sent to the client.
And the javascript around it is executed at the client.
So actually you are sending this JavaScript code to be executed to the Browser:
And yes you are doing it fundamentally wrong 🙂
What you actually want to do is send a AJAX call or some other form of message back to the server to persist the model change there.