最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Ruby: check for nil in javascript - Stack Overflow

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far