admin管理员组

文章数量:1026989

I've got a getJSON script that should respond if a Ruby value is not nil, but I'm not sure how to check for if not (ruby's version of) nil through javascript. So far it looks like this:

$.getJSON("<%= xhr_document_path(document) %>", function(d) {
    if (d.ok) {
        $('#modal_<%= document.id %>').modal('show');
        clearInterval(doc_modal_<%= document.id %>);
    } else if (d.error != '') {
        window.location.href = "<%= error_document_path(document) %>";
    }
});

But the else if condition is tripped when the value is nil. How do I check for not nil here?

I've got a getJSON script that should respond if a Ruby value is not nil, but I'm not sure how to check for if not (ruby's version of) nil through javascript. So far it looks like this:

$.getJSON("<%= xhr_document_path(document) %>", function(d) {
    if (d.ok) {
        $('#modal_<%= document.id %>').modal('show');
        clearInterval(doc_modal_<%= document.id %>);
    } else if (d.error != '') {
        window.location.href = "<%= error_document_path(document) %>";
    }
});

But the else if condition is tripped when the value is nil. How do I check for not nil here?

Share Improve this question edited Apr 1, 2014 at 7:42 t56k asked Apr 1, 2014 at 7:36 t56kt56k 7,03110 gold badges58 silver badges121 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

If d was converted from Ruby to JavaScript through JSON, then the Ruby nil would be converted to JavaScript null.

I've got a getJSON script that should respond if a Ruby value is not nil, but I'm not sure how to check for if not (ruby's version of) nil through javascript. So far it looks like this:

$.getJSON("<%= xhr_document_path(document) %>", function(d) {
    if (d.ok) {
        $('#modal_<%= document.id %>').modal('show');
        clearInterval(doc_modal_<%= document.id %>);
    } else if (d.error != '') {
        window.location.href = "<%= error_document_path(document) %>";
    }
});

But the else if condition is tripped when the value is nil. How do I check for not nil here?

I've got a getJSON script that should respond if a Ruby value is not nil, but I'm not sure how to check for if not (ruby's version of) nil through javascript. So far it looks like this:

$.getJSON("<%= xhr_document_path(document) %>", function(d) {
    if (d.ok) {
        $('#modal_<%= document.id %>').modal('show');
        clearInterval(doc_modal_<%= document.id %>);
    } else if (d.error != '') {
        window.location.href = "<%= error_document_path(document) %>";
    }
});

But the else if condition is tripped when the value is nil. How do I check for not nil here?

Share Improve this question edited Apr 1, 2014 at 7:42 t56k asked Apr 1, 2014 at 7:36 t56kt56k 7,03110 gold badges58 silver badges121 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

If d was converted from Ruby to JavaScript through JSON, then the Ruby nil would be converted to JavaScript null.

本文标签: Ruby check for nil in javascriptStack Overflow