Pass boolean from rails controller to javascript for use in conditional
I have a form in bootstrap module which is processed with ajax. If the form validates, it closes the modal or shows validation errors as expected. However, I would like to redirect the user after the animation of the modal to hide if the condition is met based on the boolean stored in the controller. Although the condition does not work as written, it allows you to see what I am trying to accomplish:
$(document).ready(function() {
$('#modal-window').modal({remote: true});
$('#modal-window').modal('show');
$('#modal-window').on('hidden', function(){
var saved = <%= @bool %>;
if(saved == "true"){
$(window.location.replace("<%= some_url %>"));}
});
})
You are setting the save to boolean (maybe whatever is in your @bool
var)
var saved = <%= @bool %>;
but then compare with the string "true"
if(saved == "true"){
so if you replace the second line with
if(saved){
it will work
replace
var saved = <%= @bool %>;
from
var saved = "<%= @bool %>";
Where did you put this code? If your form is under index.html.erb
, then on use remote: true
you will need a file index.js.erb
that will serve the javascript that you want to run in your example. Also, be sure to avoid the return URL with j
like <%=j some_url %>
.