Javascript Gon插件未刷新-Rails 4 Partial在使用查找时未刷新

Javascript Gon插件未刷新-Rails 4 Partial在使用查找时未刷新,javascript,ruby-on-rails-4,Javascript,Ruby On Rails 4,我在这里使用本教程将dropzonejs集成到我的rails应用程序中: 它按原样工作,当我从另一个控制器(汽车)引用它时,它按原样工作。问题是我希望它只显示与当前汽车的汽车id匹配的图像。我将car.id全部设置为与images表的car_id字段相匹配,从dropzonejs字段上传的内容将其添加到数据库中,效果很好,但是当我尝试过滤部分内容时,我无法在添加新图像时使其刷新 我有两个控制器正在测试这个。图像: class ImagesController < Applicati

我在这里使用本教程将dropzonejs集成到我的rails应用程序中:

它按原样工作,当我从另一个控制器(汽车)引用它时,它按原样工作。问题是我希望它只显示与当前汽车的汽车id匹配的图像。我将car.id全部设置为与images表的car_id字段相匹配,从dropzonejs字段上传的内容将其添加到数据库中,效果很好,但是当我尝试过滤部分内容时,我无法在添加新图像时使其刷新

我有两个控制器正在测试这个。图像:

    class ImagesController < ApplicationController

  def index
    @images = Image.all
  end

  def create
    @image = Image.new(image_params)

    if @image.save
      render json: { message: "success", fileID: @image.id }, :status => 200
    else
      render json: { error: @image.errors.full_messages.join(',')}, :status => 400
    end     
  end

private

  def image_params
    params.require(:image).permit(:avatar, :car_id)
  end

end
这里是views/images/index.js.erb

$(".index").html("<%= escape_javascript(render('index')) %>")
在application.html.erb中的includes的顶部,我添加了:

def show
    gon.watch.carid = @car.id
  end
 <%= include_gon(:watch => true, :init => true) %>
//禁用自动发现 Dropzone.autoDiscover=false

  var dropzone = new Dropzone (".dropzone", {
    maxFilesize: 256, // set the maximum file size to 256 MB
    paramName: "image[avatar]", // Rails expects the file upload to be something like model[field_name]
    addRemoveLinks: false // don't show remove links on dropzone itself.
  });

  dropzone.on("success", function(file) {
    this.removeFile(file);
    $.getScript(gon.watch.carid);
  })
});
所以这里有一个新问题,如果我将图像添加到car.id“1”,然后返回到car索引并单击car.id“2”的显示链接,当我通过drop区域添加新图像时,汽车库中的汽车从car.id“2”切换到car.id“1”,直到我在页面上单击refresh,它然后重置gon.watch.carid中的变量

我假设调用show方法来显示第二辆车应该重置gon变量,这显然是错误的?如果不是application.html.erb文件中的gon watch和init调用,我认为应该这样做


有没有人使用过Gon或者知道如何解决这个问题?

我可以通过将Gon include语句从application.html.erb文件的头部移动到正文来解决这个问题,现在一切都好了

希望这对其他人有帮助

d

<% Image.where(car_id: @car.id).find_each do |image| %>
    <div class="img-thumbnail">
      <%= image_tag image.avatar.url(:thumb), alt: image.avatar.url(:thumb) %>
    </div>
  <% end %>
def show
    gon.watch.carid = @car.id
  end
 <%= include_gon(:watch => true, :init => true) %>
$(document).ready(function(){
  var dropzone = new Dropzone (".dropzone", {
    maxFilesize: 256, // set the maximum file size to 256 MB
    paramName: "image[avatar]", // Rails expects the file upload to be something like model[field_name]
    addRemoveLinks: false // don't show remove links on dropzone itself.
  });

  dropzone.on("success", function(file) {
    this.removeFile(file);
    $.getScript(gon.watch.carid);
  })
});