您好我正在构建一个带有rails和angular的应用程序。我继续在执行此代码时出现类型错误
$scope.makeTip = function(tip){
data = {
tip: {
bookies: tip.bookies,
user_id: $scope.currentUser.id
},
prediction: $scope.madePredictions
},
$http.post('/tips.json', data).success(function(data){
console.log(data)
});
};
$http.get('/predictions/fixtures_this_week').success(function(response){
$scope.fixturesThisWeek = response.data;
});
//Updating the scores the dependent of on the type of bet id
$scope.addPrediction = function(prediction, fixtureId) {
data = {};
data.fixtureId = fixtureId;
data.predictionGoalsHomeTeam = prediction.scores.predictionGoalsHomeTeam[fixtureId];
data.predictionGoalsAwayTeam = prediction.scores.predictionGoalsAwayTeam[fixtureId];
data.typeOfBet = prediction.typeOfBetId[fixtureId];
$scope.madePredictions.push(data);
console.log($scope.madePredictions)
}
}]);
但是我认为问题源于我的提示控制器中的方法,可能是在第4行
def create
@tip = Tip.new(params[:tip])
@tip.save
@prediction = Prediction.find(params[:prediction][:fixtureId])
@prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsHomeTeam]
@prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsAwayTeam]
@prediction.save
@tip.predictions << @prediction
respond_with(@tip)
end
有没有人知道如何处理这种类型错误?
由于
答案 0 :(得分:1)
一些错误..在你的create方法中,你连续两次分配@ prediction.predictionGoalsHomeTeam。
@prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsHomeTeam]
@prediction.predictionGoalsHomeTeam = params[:prediction][:predictionGoalsAwayTeam]
因此,您需要将最后一个更改为AwayTeam,因为这是您从params获得的。
你的主要问题是params [:prediction]是一个数组,所以Prediction.find(params[:prediction][:fixtureId])
不会工作,因为你试图抓住那个不存在的数组的fixtureId。您需要遍历params[:prediction]
并分别在@tip.predictions
中存储每个对象。试试这个:
def create
@tip = Tip.new(params[:tip])
@tip.save
params[:prediction].each do |p|
@prediction = Prediction.find(p[:fixtureId])
@prediction.predictionGoalsHomeTeam = p[:predictionGoalsHomeTeam]
@prediction.predictionGoalsAwayTeam = p[:predictionGoalsAwayTeam]
@prediction.save
@tip.predictions << @prediction
end
respond_with(@tip)
end