jjzjj

javascript - 出现错误 - 调试连接已关闭。原因 : Render process gone

coder 2025-01-05 原文

我正在制作游戏,但最终它会停止工作。似乎没有任何韵律或原因,不确定我是否有内存泄漏或类似的问题?在我插入大量数组之前,我是从一个在线托管的 json 文件中提取它的。直到我完成游戏后才在控制台中显示消息。 “调试连接已关闭。原因:渲染进程消失了。”不管是 firefox 还是 chrome。由于点击按钮很重,似乎如果我点击的速度不那么快,它就会运行。

<html>
<head>
<script
  src="https://code.jquery.com/jquery-3.2.1.js"
  integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
  crossorigin="anonymous"></script>
<style>
#myScore {
    position: fixed;
    right: 5px;
    width: 220px;
    border: 0px solid #555555;
    padding: 10px;
}
#myContainer {
  width: 736px;
  height: 736px;
  position: relative;
  background-image:url(img/board.jpg);
}
#unit1 {
  width: 10px;
  height: 10px;
  position: absolute;
  background-color: red;
  left: 20;
  top: 10;
}
#unit2 {
  width: 10px;
  height: 10px;
  position: absolute;
  background-color: red;
  left: 80;
  top: 10;
}
#unit3 {
  width: 10px;
  height: 10px;
  position: absolute;
  background-color: red;
  left: 120;
  top: 10;
}
#unit4 {
  width: 10px;
  height: 10px;
  position: absolute;
  background-color: red;
  left: 180;
  top: 10;
}
#unit5 {
  width: 10px;
  height: 10px;
  position: absolute;
  background-color: blue;
  left: 500;
  top: 20;
}
#unit6 {
  width: 10px;
  height: 10px;
  position: absolute;
  background-color: blue;
  left: 550;
  top: 20;
}
#unit7 {
  width: 10px;
  height: 10px;
  position: absolute;
  background-color: blue;
  left: 670;
  top: 20;
}
#unit8 {
  width: 10px;
  height: 10px;
  position: absolute;
  background-color: blue;
  left: 720;
  top: 20;
}
#player1 {  background-color: red;}
#player2 {  background-color: blue;}
</style>
</head>
<body>
<div id="myScore"> 
<table align="right" width="100%"><tr><td id="player1">Team 1</td>
<td id="player2">Team 2</td></tr><tr><td id="team1"></td><td id="team2"></td></tr>
</table>
<tr><td>
<p id="1st">1st Dice<br><div id ="dice1"></div><div id ="1stmove"></div></p>
<p id="2nd">2nd Dice<br><div id ="dice2"></div><div id ="2ndmove"></div></p>
</td>
<td><button id="turn" onclick="rollem()">Player 1 Starts</button>
<button id="turn" onclick="lastmove1(unit6)">Unit1 last move</button></td></tr>
<button id="turn" onclick="bumpmove(unit1, '75')">Unit1 bumpmove</button></td></tr>
<button id="turn" onclick="bumpmove(unit2, '75')">Unit2 bumpmove</button></td></tr>
<button id="turn" onclick="bumpmove(unit3, '75')">Unit3 bumpmove</button></td></tr>
<button id="turn" onclick="bumpmove(unit4, '75')">Unit4 bumpmove</button></td></tr>
<br>
<p id="total"></p>
<p id="list"></p>
<p id="change"></p>
<p id="update"></p>
</div>
<div id ="myContainer">
<div id ="unit1"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/unit1.png" width="20px" height="20px"/></div>
<div id ="unit2"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/unit2.png" width="20px" height="20px"/></div>
<div id ="unit3"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/unit3.png" width="20px" height="20px"/></div>
<div id ="unit4"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/unit4.png" width="20px" height="20px"/></div>
<div id ="unit5"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/unit5.png" width="20px" height="20px"/></div>
<div id ="unit6"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/unit6.png" width="20px" height="20px"/></div>
<div id ="unit7"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/unit7.png" width="20px" height="20px"/></div>
<div id ="unit8"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/unit8.png" width="20px" height="20px"/></div>
</div>
<script>
"use strict";
function bigImg(x) {
    x.style.height = "64px";
    x.style.width = "64px";
}
function normalImg(x) {
    x.style.height = "20px";
    x.style.width = "20px";
}
var turn = "1";
var data = [{"FIELD1":425,"FIELD2":715},
{"FIELD1":425,"FIELD2":680},
{"FIELD1":425,"FIELD2":645},
{"FIELD1":425,"FIELD2":615},
{"FIELD1":425,"FIELD2":580},
{"FIELD1":425,"FIELD2":545},
{"FIELD1":425,"FIELD2":515},
{"FIELD1":425,"FIELD2":480},
{"FIELD1":480,"FIELD2":480},
{"FIELD1":510,"FIELD2":480},
{"FIELD1":545,"FIELD2":480},
{"FIELD1":580,"FIELD2":480},
{"FIELD1":610,"FIELD2":480},
{"FIELD1":645,"FIELD2":480},
{"FIELD1":680,"FIELD2":480},
{"FIELD1":710,"FIELD2":480},
{"FIELD1":710,"FIELD2":380},
{"FIELD1":710,"FIELD2":280},
{"FIELD1":680,"FIELD2":280},
{"FIELD1":645,"FIELD2":280},
{"FIELD1":610,"FIELD2":280},
{"FIELD1":580,"FIELD2":280},
{"FIELD1":545,"FIELD2":280},
{"FIELD1":510,"FIELD2":280},
{"FIELD1":480,"FIELD2":280},
{"FIELD1":450,"FIELD2":260},
{"FIELD1":450,"FIELD2":230},
{"FIELD1":450,"FIELD2":200},
{"FIELD1":450,"FIELD2":160},
{"FIELD1":450,"FIELD2":130},
{"FIELD1":450,"FIELD2":100},
{"FIELD1":450,"FIELD2":60},
{"FIELD1":450,"FIELD2":30},
{"FIELD1":360,"FIELD2":30},
{"FIELD1":260,"FIELD2":30},
{"FIELD1":260,"FIELD2":60},
{"FIELD1":260,"FIELD2":90},
{"FIELD1":260,"FIELD2":120},
{"FIELD1":260,"FIELD2":160},
{"FIELD1":260,"FIELD2":190},
{"FIELD1":260,"FIELD2":220},
{"FIELD1":280,"FIELD2":260},
{"FIELD1":260,"FIELD2":280},
{"FIELD1":230,"FIELD2":280},
{"FIELD1":200,"FIELD2":280},
{"FIELD1":160,"FIELD2":280},
{"FIELD1":130,"FIELD2":280},
{"FIELD1":100,"FIELD2":280},
{"FIELD1":60,"FIELD2":280},
{"FIELD1":20,"FIELD2":280},
{"FIELD1":20,"FIELD2":380},
{"FIELD1":20,"FIELD2":480},
{"FIELD1":50,"FIELD2":460},
{"FIELD1":80,"FIELD2":460},
{"FIELD1":110,"FIELD2":460},
{"FIELD1":160,"FIELD2":460},
{"FIELD1":200,"FIELD2":460},
{"FIELD1":240,"FIELD2":460},
{"FIELD1":250,"FIELD2":460},
{"FIELD1":280,"FIELD2":480},
{"FIELD1":280,"FIELD2":500},
{"FIELD1":280,"FIELD2":540},
{"FIELD1":280,"FIELD2":580},
{"FIELD1":280,"FIELD2":610},
{"FIELD1":280,"FIELD2":640},
{"FIELD1":280,"FIELD2":680},
{"FIELD1":280,"FIELD2":710},
{"FIELD1":370,"FIELD2":715},
{"FIELD1":680,"FIELD2":360},
{"FIELD1":650,"FIELD2":360},
{"FIELD1":620,"FIELD2":360},
{"FIELD1":580,"FIELD2":360},
{"FIELD1":550,"FIELD2":360},
{"FIELD1":520,"FIELD2":360},
{"FIELD1":480,"FIELD2":360},
{"FIELD1":430,"FIELD2":360},
{"FIELD1":360,"FIELD2":50},
{"FIELD1":360,"FIELD2":80},
{"FIELD1":360,"FIELD2":120},
{"FIELD1":360,"FIELD2":150},
{"FIELD1":360,"FIELD2":180},
{"FIELD1":360,"FIELD2":220},
{"FIELD1":360,"FIELD2":250},
{"FIELD1":360,"FIELD2":300},
{"FIELD1":120,"FIELD2":10},
{"FIELD1":120,"FIELD2":10},
{"FIELD1":120,"FIELD2":10},
{"FIELD1":120,"FIELD2":10},
{"FIELD1":120,"FIELD2":10},
{"FIELD1":120,"FIELD2":10},
{"FIELD1":120,"FIELD2":10},
{"FIELD1":120,"FIELD2":10}]

var unit1,unit2,unit3,unit4,unit5,unit6,unit7,unit8;
unit1 = { "name":"unit1", "curPos":1, "startPos":38, "jail":"True", "last":"False"};
unit2 = { "name":"unit2", "curPos":1, "startPos":38, "jail":"True", "last":"False"};
unit3 = { "name":"unit3", "curPos":1, "startPos":38, "jail":"True", "last":"False"};
unit4 = { "name":"unit4", "curPos":1, "startPos":38, "jail":"True", "last":"False"};
unit5 = { "name":"unit5", "curPos":2, "startPos":22, "jail":"True", "last":"False"};
unit6 = { "name":"unit6", "curPos":2, "startPos":22, "jail":"True", "last":"False"};
unit7 = { "name":"unit7", "curPos":2, "startPos":22, "jail":"True", "last":"False"};
unit8 = { "name":"unit8", "curPos":2, "startPos":22, "jail":"True", "last":"False"};

var team1, team2;
team1 = [unit1,unit2,unit3,unit4];
team2 = [unit5,unit6,unit7,unit8];

function team(a) {
for (i = 0; i < a.length; i++) {
    var x = a[i].curPos = "38"; 
    } 
}

function bumpmove(a, d) {
    var cur = a.curPos;
    d = Number(d);
    cur = Number(cur);
    cur = cur + d;
    cur = Number(cur);
    a.curPos = cur;
        var obj = data[cur];
        var current = Object.values(obj);
        var elem = document.getElementById(a.name); 
        elem.style.left = current[0] + 'px';
        elem.style.top = current[1] + 'px';
    }

function lastmove1(a) {
    var d = document.getElementById("1st").innerHTML;
    var cur = a.curPos;
    d = Number(d);
    cur = Number(cur);
    cur = cur + d;
    cur = Number(cur);
    a.curPos = cur;
        var obj = data[cur];
        var current = Object.values(obj);
        var elem = document.getElementById(a.name); 
        elem.style.left = current[0] + 'px';
        elem.style.top = current[1] + 'px';
    }
function lastmove2(a) {
    var d = document.getElementById("1st").innerHTML;
    var cur = a.curPos;
    d = Number(d);
    cur = Number(cur);
    cur = cur + d;
    cur = Number(cur);
    a.curPos = cur;
        console.log(cur)
        var obj = data[cur];
        console.log(obj)
        var current = Object.values(obj);
        var elem = document.getElementById(a.name); 
        elem.style.left = current[0] + 'px';
        elem.style.top = current[1] + 'px';
    }

function check1(a) {
for (i = 0; i < team1.length; i++) {
    var x = team1[i];
    if (x.curPos == a.curPos) {
        if (x.name != a.name){
        var num = x.curPos;
        num = Number(num);
        console.log(num)
        var obj = data[num];
        console.log(obj)
        var current = Object.values(obj);   
        var elem = x.name;
        document.getElementById(elem).style.left = current[0] += 20 ;
        document.getElementById(elem).style.top = current[1] ;  
        }
    }else {
        console.log()
        }
    }
for (i = 0; i < team2.length; i++) {
    var x = team2[i];
    if (x.curPos == a.curPos) {
        x.jail = "True"
        var elem = x.name;
        if (elem == "unit5") {
        document.getElementById(elem).style.left = "500" ;
        document.getElementById(elem).style.top = "20" ;    
        }
        else if (elem == "unit6") {
        document.getElementById(elem).style.left = "550" ;
        document.getElementById(elem).style.top = "20" ;    
        }
        else if (elem == "unit7") {
        document.getElementById(elem).style.left = "670" ;
        document.getElementById(elem).style.top = "20" ;    
        }
        else if (elem == "unit8") {
        document.getElementById(elem).style.left = "720" ;
        document.getElementById(elem).style.top = "20" ;    
        }
        else {
        console.log()
        }

    }else {
        console.log()
        }
    }   
}

function check2(a) {
for (i = 0; i < team2.length; i++) {
    var x = team2[i];
    if (x.curPos == a.curPos) {
        if (x.name != a.name){
        var num = x.curPos;
        num = Number(num);
        console.log(num)
        var obj = data[num];
        console.log(obj)
        var current = Object.values(obj);   
        var elem = x.name;
        document.getElementById(elem).style.left = current[0] += 20 ;
        document.getElementById(elem).style.top = current[1] ;  
        }
    }else {
        console.log()
        }
    }
for (i = 0; i < team1.length; i++) {
    var x = team1[i];
    if (x.curPos == a.curPos) {
        x.jail = "True"
        var elem = x.name;
        if (elem == "unit1") {
        document.getElementById(elem).style.left = "20" ;
        document.getElementById(elem).style.top = "20" ;    
        }
        else if (elem == "unit2") {
        document.getElementById(elem).style.left = "80" ;
        document.getElementById(elem).style.top = "20" ;    
        }
        else if (elem == "unit3") {
        document.getElementById(elem).style.left = "120" ;
        document.getElementById(elem).style.top = "20" ;    
        }
        else if (elem == "unit4") {
        document.getElementById(elem).style.left = "180" ;
        document.getElementById(elem).style.top = "20" ;    
        }
        else {
        console.log()
        }

    }else {
        console.log()
        }
    }   
}   

//makes the 1st dice move happen, must specify team
function mv1(a) {
if (a.jail == "False"){
    document.getElementById("1stmove").innerHTML = "" ;
    var d = document.getElementById("1st").innerHTML;
    var cur = a.curPos;
    d = Number(d);
    cur = Number(cur);
    cur = cur + d;
    a.curPos = cur;
        if (cur >= "83" && a.name == a.name && a.last == "True"){
        console.log("removed " + a.name)

        Array.prototype.remByVal = function(val) {
            for (var i = 0; i < this.length; i++) {
                if (this[i] === val) {
                    this.splice(i, 1);
                    i--;
                }
            }
            return this;
        }
        team1 = team1.remByVal(a)
        console.log(team1)
        }
        else if (cur >= "76" && a.name == a.name && a.last == "True"){
            console.log(cur + " almost there")
        }
        else if (cur >= "68") {
        cur = cur - "68";
            if (cur >= "34" && a.name == a.name) {
            console.log(cur)
            cur = "76";
            a.last = "True";
            a.curPos = cur;
            }
        }
        console.log(cur)
        var obj = data[cur];
        var current = Object.values(obj);
        var elem = document.getElementById(a.name); 
        elem.style.left = current[0] + 'px';
        elem.style.top = current[1] + 'px';
    }
}
//makes the 2nd dice move happen, must specify team
function mv2(a) {
if (a.jail == "False"){
    document.getElementById("2ndmove").innerHTML = "" ;
    var d = document.getElementById("2nd").innerHTML;
    var cur = a.curPos;
    d = Number(d);
    cur = Number(cur);
    cur = cur + d;
    a.curPos = cur;
        if (cur >= "83" && a.name == a.name && a.last == "True"){
        console.log("removed " + a.name)

        Array.prototype.remByVal = function(val) {
            for (var i = 0; i < this.length; i++) {
                if (this[i] === val) {
                    this.splice(i, 1);
                    i--;
                }
            }
            return this;
        }
        team1 = team1.remByVal(a)
        console.log(team1)
        }
        else if (cur >= "76" && a.name == a.name && a.last == "True"){
            console.log(cur + " almost there")
        }
        else if (cur >= "68") {
        cur = cur - "68";
            if (cur >= "34" && a.name == a.name) {
            console.log(cur)
            cur = "76";
            a.last = "True";
            a.curPos = cur;
            }
        }
        console.log(cur)
        var obj = data[cur];
        var current = Object.values(obj);
        var elem = document.getElementById(a.name); 
        elem.style.left = current[0] + 'px';
        elem.style.top = current[1] + 'px';
    }
}
//Rolls, get's random, get's results and move accordingly
function rollem() {
    var dice1 = Math.floor((Math.random() * 3)+1);
    var dice2 = Math.floor((Math.random() * 3)+1);  
    document.getElementById("1st").innerHTML = dice1;
    document.getElementById("2nd").innerHTML = dice2;
    var total = Number(dice1) + Number(dice2);
    document.getElementById("total").innerHTML = total + " is the Total you can move";
    document.getElementById("dice1").innerHTML = '<img src= "img/' + dice1 + '.jpg" width="30px" height="30px"/>';
    document.getElementById("dice2").innerHTML = '<img src= "img/' + dice2 + '.jpg" width="30px" height="30px"/>';
    if (dice1 === dice2 && turn == "1") {
        turn = "1";
        var i;
        for (i = 0; i < team1.length; i++) {
            var x = team1[i].name;
            if (team1[i].jail == "True") {
                team1[i].jail = "False";
                var obj = data[38];
                team1[i].curPos = "38";
                var current = Object.values(obj);
                var elem = document.getElementById(x); 
                elem.style.left = current[0] + 'px';
                elem.style.top = current[1] + 'px';
                document.getElementById("1stmove").innerHTML += '<button src= "img/' + x + '.png" onclick="mv1('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                + x +'.png" width="20px" height="20px"/> </button>';
                document.getElementById("2ndmove").innerHTML += '<button src= "img/' + x + '.png" onclick="mv2('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                + x +'.png" width="20px" height="20px"/> </button>';

            }else {
//          var d = document.getElementById("2nd").innerHTML;
            turn = "1";
            var i;
            for (i = 0; i < team1.length; i++) {
                var x = team1[i].name;
                if (team1[i].jail == "False") {
                    document.getElementById("1stmove").innerHTML += '<button onclick="mv1('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                    + x +'.png" width="20px" height="20px"/> </button>';
                    document.getElementById("2ndmove").innerHTML += '<button onclick="mv2('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                    + x +'.png" width="20px" height="20px"/> </button>';
                    document.getElementById("turn").innerHTML = "Player " + turn + " 's turn!"
                }else {
                document.getElementById("turn").innerHTML = "Player " + turn + " 's turn!"
                }
            }   
            //check2(x);    
            }
        }   
    }
    else if (dice1 === dice2 && turn == "2") {
        turn = "2";
        var i;
        for (i = 0; i < team2.length; i++) {
            var x = team2[i].name;
            if (team2[i].jail == "True") {
                team2[i].jail = "False";
                var obj = data[22];
                team2[i].curPos = "22";
                var current = Object.values(obj);
                var elem = document.getElementById(x); 
                elem.style.left = current[0] + 'px';
                elem.style.top = current[1] + 'px';
                document.getElementById("1stmove").innerHTML += '<button src= "img/' + x + '.png" onclick="mv1('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                + x +'.png" width="20px" height="20px"/> </button>';    
                document.getElementById("2ndmove").innerHTML += '<button src= "img/' + x + '.png" onclick="mv2('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                + x +'.png" width="20px" height="20px"/> </button>';                

            }else {
//          var d = document.getElementById("2nd").innerHTML;
            turn = "2";
            var i;
            for (i = 0; i < team1.length; i++) {
                var x = team2[i].name;
                if (team2[i].jail == "False") {
                    document.getElementById("1stmove").innerHTML += '<button onclick="mv1('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                    + x +'.png" width="20px" height="20px"/> </button>';
                    document.getElementById("2ndmove").innerHTML += '<button onclick="mv2('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                    + x +'.png" width="20px" height="20px"/> </button>';
                    document.getElementById("turn").innerHTML = "Player " + turn + " 's turn!"
                }else {
                document.getElementById("turn").innerHTML = "Player " + turn + " 's turn!"
                }
            }   
            //check1(x);    
            }
        }   
    }
    else if (turn == "1") {
        turn = "2";
        var i;
        for (i = 0; i < team1.length; i++) {
            var x = team1[i].name;
            if (team1[i].jail == "False") {
                document.getElementById("1stmove").innerHTML += '<button onclick="mv1('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                + x +'.png" width="20px" height="20px"/> </button>';
                document.getElementById("2ndmove").innerHTML += '<button onclick="mv2('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
                + x +'.png" width="20px" height="20px"/> </button>';
                document.getElementById("turn").innerHTML = "Player " + turn + " 's turn!"
            }else {
            document.getElementById("turn").innerHTML = "Player " + turn + " 's turn!"
            }
        }   
    }
    else if (turn == "2"){
        turn = "1";
        var i;
        for (i = 0; i < team2.length; i++) {
            var x = team2[i].name;
            if (team2[i].jail == "False") {
            document.getElementById("1stmove").innerHTML += '<button onclick="mv1('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
            + x +'.png" width="20px" height="20px"/> </button>';
            document.getElementById("2ndmove").innerHTML += '<button onclick="mv2('+x+')"><img onmouseover="bigImg(this)" onmouseout="normalImg(this)" src= "img/' 
            + x +'.png" width="20px" height="20px"/> </button>';
            document.getElementById("turn").innerHTML = "Player " + turn + " 's turn!"
            }else {
            document.getElementById("turn").innerHTML = "Player " + turn + " 's turn!"
            }
        }   
    }       
}
</script>
</body>
</html>

最佳答案

我有这个错误,它看起来是随机的,但我找到了原因的根源:一个永无止境的 for环形。

鉴于 Google Chrome 中错误的非特异性,我不得不写一堆 console.log("...")在这个问题上将注释归零,发现我(糟糕地)编写了一个既有非整数增量又有可能永远不会到达循环末尾的函数。

看起来像这样:

var myArray = [];
for (i = 0; i <= total; i += total / 100) {
   myArray.push(i);
};

由于四舍五入问题,i总是 <= total .我更新了代码以改为使用:

var myArray = [];
for (var i = 0; i <= 100; i++) {
   myArray.push(total * (i / 100));
};

从那以后就没有问题了。

对于 OP,虽然我没有深入研究您的代码,但它可能是您的 for 之一减少 迭代器 ( i ) 的循环可能会创建相同的永无止境的循环。我指的是您代码的这一部分:

Array.prototype.remByVal = function(val) {
   for (var i = 0; i < this.length; i++) {
      if (this[i] === val) {
         this.splice(i, 1);
         i--;  // <-- This part here could be an issue
      }
   }
   return this;
}

关于javascript - 出现错误 - 调试连接已关闭。原因 : Render process gone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46855911/

有关javascript - 出现错误 - 调试连接已关闭。原因 : Render process gone的更多相关文章

  1. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  2. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  3. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  4. ruby - 无法在 60 秒内获得稳定的 Firefox 连接 (127.0.0.1 :7055) - 2

    我使用的是Firefox版本36.0.1和Selenium-Webdrivergem版本2.45.0。我能够创建Firefox实例,但无法使用脚本继续进行进一步的操作无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055)错误。有人能帮帮我吗? 最佳答案 我遇到了同样的问题。降级到firefoxv33后一切正常。您可以找到旧版本here 关于ruby-无法在60秒内获得稳定的Firefox连接(127.0.0.1:7055),我们在StackOverflow上找到一个类

  5. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  6. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

  7. ruby-on-rails - 错误 : Error installing pg: ERROR: Failed to build gem native extension - 2

    我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby​​'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe

  8. ruby - #之间? Cooper 的 *Beginning Ruby* 中的错误或异常 - 2

    在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee

  9. ruby-on-rails - 每次我尝试部署时,我都会得到 - (gcloud.preview.app.deploy) 错误响应 : [4] DEADLINE_EXCEEDED - 2

    我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie

  10. ruby-on-rails - Rails 5 Active Record 记录无效错误 - 2

    我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa

随机推荐