I have a form and I want to update the show_msg parameter (it’s a boolean parameter) while the user press the submit button. if the user checked the box and press submit, I want to set it TRUE. otherwise: FALSE.
so I have the main html that has a popup window. in this popup window I wrote my form. (what should I have the write in ‘action’? I just have to close this popup window):
<form name="input" action="#" method="post">
<input type="checkbox" id="mycheckbox">Don't show me again<br>
<input type="submit" id="Submit">
</form>
this is my javascript:
$("#submit").click(function() {
$.ajax({
## the url is the controller
url: '/welcome',
type: 'PUT',
data: {show_msg: $("#mycheckbox").is(":checked")}
});
});
and this is my controller:
class WelcomeController < ApplicationController
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:show_msg])
format.html { redirect_to @user, notice: 'You will never see this message again.' }
format.json { head :no_content }
end
end
end
any help appreciated!
Build the form with the Rails
form_fortag.Then instead of trigger an event on Submit button click, trigger on Form submit.
Finally, in you
$.ajax call, for theurlattribute, retrieve the good URL from the form action attribute.For information, with the form tag, you can add a
remote: trueoption. And then bind an event onajax:success,ajax:erroretc.