我正在尝试使用 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>