3

I have a form in a bootstrap modal that is being processed using ajax. If the form validates it closes the modal or shows the validation errors as expected. However, I would like to redirect the user after the modal hide animation is finished if a condition is met based on a boolean held in the controller. Although the conditional wouldn't work as written, it lets you see what it is 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 Answers 3

3

You're setting saved to a boolean (probably - whatever is in your @bool var)

var saved = <%= @bool %>;

but then compare to the string "true"

if(saved == "true"){

so, if you replace the second line with

if(saved){

it'll work

Sign up to request clarification or add additional context in comments.

1 Comment

whoops. thanks for pointing out the mistake. shweta's works too but this had a little more explanation.
1

replace

var saved = <%= @bool %>;

with

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

Comments

0

Where do you have this code posted? If your form is under index.html.erb then when using remote: true, you will want to have a index.js.erb file that will serve the javascript that you're looking to run in your example. Also, don't forget to escape the return URL with j like <%=j some_url %>.

2 Comments

Currently this in a <script> in the index.html.erb. I have the form updating any validation errors in the index.js.erb. Wouldn't I want the .on('hidden'... in the index.html.erb since it's going to be listening for the modal to finish closing to perform the redirect?
I'm new to js and i just don't know how to get a rails variable passed into the script above in order to make the conditional work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.