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 %>"));}
    });
})

      

+3


source to share


3 answers


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

+1


source


replace

var saved = <%= @bool %>;

      



from

var saved = "<%= @bool %>";

      

+1


source


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 %>

.

0


source







All Articles