我目前在Javascript,Rails,JQuery应用程序中遇到第三个调试器断点时遇到了一些困难。它击中前两个就好了,但没有击中第三个。有谁知道为什么会出现这种情况?提前致谢!这是我正在使用的代码:
user_index.js
$(document).ready(function () {
$(".loadCars").click(function(event) {
event.preventDefault()
var url = this.href
debugger
$.getJSON(url)
.success(function(data) {
$('#cars').html("");
debugger
$.each(data, function(index, value) {
var newCar = new Car(value, url);
var carHtml = newCar.formatDisplay();
$('#cars').append(carHtml)
debugger
});
});
});
});
function Car(car, url) {
this.id = car.id
this.make = car.make
this.model = car.model
this.awards = car.awards
};
Car.prototype.formatDisplay = function() {
var awardsHtml = ""
$.each(this.awards,function() {
var awardListItem = this.formatDisplay()
awardsHtml += awardListItem
});
var carHtml = `<div><a href='${this.carUrl}'>${this.make} + " " + ${this.model} </a></div>
<div>Awards:</div>
<ul>${awardsHtml}</ul>`;
return carHtml;
};
function Award(award) {
this.id = award.id
this.title = award.title
this.description = award.description
};
Award.prototype.formatDisplay = function() {
var awardHtml = `<li>Award: ${this.title}, Description: ${this.description}</li>`
return awardHtml;
};
users_controller.rb
class UsersController < ApplicationController
def new
@user = User.new
end
def index
@users = User.all
end
def show
@user = current_user
@cars = @user.cars
end
def destroy
session.destroy
redirect_to root_path
end
def cars_index
@user = User.find(params[:id])
@cars = @user.cars
render template: 'cars/all'
end
def car_index
@user = current_user
@cars = @user.cars
render json: @cars
end
end
用户/ show.html.erb
<%= render 'layouts/header' %>
<h1>My Profile:</h1>
<h1><%= @user.email %></h1><br>
<button class="btn btn-default"><%= link_to "Load Cars", car_index_path(@user), :class => "loadCars" %></button><br><br>
<div class="cars">
<ol>
</ol>
</div>
<div id="cars">
<ol>
</ol>
</div>
如果有任何其他代码段有助于诊断问题,请告诉我。谢谢!
答案 0 :(得分:1)
奖项似乎不是奖励对象。您将需要创建Award对象来访问这些方法,就像使用Car对象一样。
function Car(car, url) {
this.id = car.id;
this.make = car.make;
this.model = car.model;
if (car.awards) {
this.awards = car.awards.map(award => new Award(award));
}
}