<%= form_for(@mymodel, remote: true, html: { id: 'match_form' }) do |f| %>
<!-- I need to check if @mymodel.match_id matches the value generated by a controller function -->
<%= f.submit 'Save', class: 'btn btn-primary', id: 'match_submit', style: "width:38px;padding:0px" %>
<%= button_tag 'Cancel', class: 'btn btn-secondary', id: 'match_cancel', style: "width:52px;padding:0px" %>
<% end%>
<script type='text/javascript'>
$(function() {
$(document).on("click", "#match_submit", function(event){
$.ajax('my_controller_method', {
type: 'GET',
dataType: 'script',
data: {
mid: $("#").val(), // how do I pass @mymodel.match_id here?
},
error: function(jqXHR, textStatus, errorThrown) {
return console.log("AJAX Error: " + textStatus);
}
});
});
</script>
I have a form as shown above.
- How do I pass @mymodel.match_id in the ajax call above?
- In my controller function, I would like to check if passed match_id is a certain value; if not, I would like to display a modal with an error message and just an
Ok
button. How can I achieve this?
< > a) Should I return the correct value from controller function and check the same in my javascript code above and show a javascript alert?
< >< > i) If yes, then how can I return a number from a controller function to my JavaScript invoking function?
or
< > b) would I be able to render a bootstrap modal with anOk
button in my controller itself?
UPDATE 1:
I tried this to pass match_id
and id
from mymodel
to the ajax call but it did not work:
$(document).on("click", "#match_submit", function(event){
alert ("match id changed!");
alert($("#match_form").data('id'));
alert($("#match_form").data('match_id'));
$.ajax('cpl_evaluate_match_id', {
type: 'GET',
dataType: 'script',
data: {
debit_id: $("#match_form").data('id'),
match_id: $("#match_form").data('match_id')
},
error: function(jqXHR, textStatus, errorThrown) {
return console.log("AJAX Error: " + textStatus);
}
});
});
The alerts to print id
and match_id
print undefined
in the alert messages.
UPDATE 2:
I tried this to return a number from my controller function but this doesn't seem to work either:
def evaluate_match_id
puts params[:debit_id]
puts params[:match_id]
return 1
end
the puts above turn up blank
$(document).on("click", "#match_submit", function(event){
alert ("match id changed!");
alert($("#match_form").data('id'));
alert($("#match_form").data('match_id'));
$.ajax('cpl_evaluate_match_id', {
type: 'GET',
dataType: 'script',
data: {
debit_id: $("#match_form").data('id'),
match_id: $("#match_form").data('match_id')
},
success: function(result){
if(result){ // yes or no? 1 or 0, it's the return coming from your controller
alert('true');
alert(result);
}
else{
alert('error');
alert(result);
}
},
error: function(jqXHR, textStatus, errorThrown) {
return console.log("AJAX Error: " + textStatus);
}
});
});
The value of result
in the success:
block in the JavaScript code above comes up as undefined
(my controller function blindly returns the number 1
which I was expecting to be the value of result
here)