Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript/JQuery中未命中调试器_Javascript_Jquery_Ruby On Rails - Fatal编程技术网

在Javascript/JQuery中未命中调试器

在Javascript/JQuery中未命中调试器,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我目前在Javascript、Rails和JQuery应用程序中遇到了一些困难,无法达到我的第三个调试器断点。它正好击中前两个,但没有击中第三个。有人知道为什么会这样吗?提前谢谢!以下是我正在使用的代码: user_index.js $(document).ready(function () { $(".loadCars").click(function(event) { event.preventDefault() var url = this.href

我目前在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;
};
$(文档).ready(函数(){
$(“.loadCars”)。单击(函数(事件){
event.preventDefault()
var url=this.href
调试器
$.getJSON(url)
.成功(功能(数据){
$('#cars').html(“”);
调试器
$.each(数据、函数(索引、值){
var newCar=新车(值,url);
var carHtml=newCar.formatDisplay();
$('#cars')。附加(carHtml)
调试器
});
});
});
});
功能车(车,网址){
this.id=car.id
这辆车
this.model=car.model
this.awards=car.awards
};
Car.prototype.formatDisplay=函数(){
var awardsHtml=“”
$.each(this.awards,function()){
var awardListItem=this.formatDisplay()
awardsHtml+=awardListItem
});
var carHtml=`
奖品:
    ${awardsHtml}
`; 返回carHtml; }; 功能奖(奖励){ this.id=award.id this.title=奖励.title this.description=奖励.description }; Award.prototype.formatDisplay=函数(){ var awardHtml=`
  • 奖励:${this.title},说明:${this.Description}
  • ` 返回awardHtml; };
    用户\u 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
    
    class UsersController
    users/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>
    
    
    我的个人资料:
    
    “载重汽车”%>


    请告诉我是否有任何其他代码片段有助于诊断问题。谢谢大家!

    看起来这些奖项不是奖励对象。您需要创建奖励对象来访问这些方法,就像您对汽车对象所做的那样

    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));
        }
    }
    

    您是否尝试过将调试器作为each中的第一条语句?Hi@Taplar感谢您的关注!我还没有试过,它确实帮我解决了这个问题!当我点击Car.prototype.formatDisplay函数中的formatDisplay函数时,似乎出现了错误。你知道我为什么会在那里出错吗?我对这个很陌生,所以我感谢你的帮助!看起来您希望
    这个。奖励
    是汽车对象吗?是吗?你们也可以在那个里抛出调试器语句:)另外,
    ${this.make}++++${this.model}
    看起来有点奇怪,因为你们在里面``明白了,我在Car.prototype.formatDisplay里放了一个调试器,每次迭代都在一个奖励对象上被调用。我为下面的奖励对象定义了我的formatDisplay,所以我想知道为什么我会得到这个。formatDisplay()不是一个函数错误。你知道为什么会这样吗?再次感谢!很高兴我们发现了您的问题,:)