0

我正在尝试使用 ajax(成功时)从 rails 5 中的控制器渲染部分内容,但在操作渲染部分内容后,空白数据将传递给 ajax。

相同的代码正在使用 rails4。我也为 rails5 更新了响应者 gem。

控制器:

 respond_with do |format|
    format.html do
      if request.xhr?
        @images = get_images
        session[:prev_images] = submitted_ids
        session[:prev_winner] = winner_id
        @prev_images = Lentil::Image.find(submitted_ids).sort_by{ |i| i.id }
        @prev_winner = session[:prev_winner]

        render :partial => "/lentil/thisorthat/battle_form", :locals => { :images => @images, :prev_images => @prev_images, :prev_winner => @prev_winner }, :layout => false, :status => :created
      end
  end

阿贾克斯:

 $(document).on('ajax:success', function (evt, data, status, xhr) {
    // Insert new images into hidden div
    $('.battle-inner:eq(1)').replaceWith(data);

    $('.battle-inner:eq(1)').imagesLoaded()
        .done(function () {
             // Remove old images
            $('.battle-wrapper:eq(0)').remove();

            // Div with new images moves up in DOM, display
            $('.battle-wrapper:eq(0)').show();

            // Hide Spinner
            $('#spinner-overlay').hide();
    });

_battle_form:

<div class="grid battle-inner">
  <%= semantic_form_for :battle, :remote => true, :url => thisorthat_result_path do |form| %>
      <% @images.each do |image| %>
      <div class="battle-image-wrap grid__cell">
      </div>
      <% end %>
 <% end %>
</div>
4

2 回答 2

0

您的请求是作为 JS 处理的吗?

尝试更换format.htmlformat.js

于 2017-11-01T00:32:44.293 回答
0

这是 jquery-ujs 的一个问题,它不再依赖于 rails>5.1。需要使用 rails-ujs 依赖项。

https://blog.bigbinary.com/2017/06/20/rails-5-1-has-dropped-dependency-on-jquery-from-the-default-stack.html

//从响应中读取数据

.on('ajax:error', function(event) {
    var detail = event.detail;
    var data = detail[0], status = detail[1],  xhr = detail[2];
    // ...
});
于 2017-11-07T20:19:52.443 回答