gpt4 book ai didi

javascript - Rails js.erb 不渲染部分文件,但获取渲染的响应文件

转载 作者:行者123 更新时间:2023-12-01 05:19:01 25 4
gpt4 key购买 nike

我有以下部分文件

_report.html.erb

<h5>REPORT</h5>
<hr/>
<%= link_to "Extract REPORT", "#", :class => 'btn btn-primary', id: 'load_panel' %>
<br/>
<br/>

<div class="row">
<% rand = Time.now.to_i - Random.new.rand(1..Time.now.to_i) %>
<div class="col-md-12">
<input type="hidden" id="randValue" value="<%= rand %>" data-validate="false" data-hidden="true"/>
<div>
<b>File Name</b>
<hr/>
</div>
<div id="application-attachments-<%= rand %>" class="col-xs-12">
<%= render partial: 'report_attachment', locals: {application_attachments: f.object.application_attachments} %>
</div>
</div>
</div>


_report_attachment.html.erb

<% application_attachments.each do |attachment| %>
<div class="fields">
<%= render "report_attachment_fields", rep_attachment: attachment, instalment: true, type: 'application_assignment' %>
</div>
<% end %>


_report_attachment_fields.html.erb

<div class="row attachment_display">
<% if rep_attachment.attachment? && rep_attachment.attachment.model.share_file %>
<% if @action == "show" && @account_type %>
<div class="col-md-6 ellipis">
<%= link_to File.basename(rep_attachment.attachment.path), rep.attachment.url, target: "_blank", id: 'view_file', :data => { application_attachment_id: rep_attachment.id } %>
<%= rep_attachment.active %>
</div>
<% end %>
<% end %>
</div>

初始加载时,它会相应地加载所有 3 个文件。但是点击提取 CTOS 后,它会发出一个 javascript 请求

application.js

$('body').on('click', '#load_panel', function(event) {
object_selected = $(this)
$(this).attr('disabled', 'disabled');

$.ajax({
type: 'GET',
url: '/applications/generate_report?application_id='+$(this).attr('data-application-id')+'&rand_value='+$(this).attr('data-rand-value'),
dataType: 'json',
success: function(data) {
object_selected.removeAttr('disabled');
}
})
})

调用 GET 请求时,它将调用此方法

def generate_report
@rand_value = params[:rand_value]
@rep_application_attachment = @application.rep_attachment
respond_to do |format|
format.js
end
end

应该调用这个 js.erb 文件

generate_report.js.erb

$("#application-attachments-#{@rand_value}").html("<%= escape_javascript(render 'report_attachment', application_attachments: @rep_application_attachment) %>");

现在的问题是我没有收到错误,它说generate_report.js.erb已呈现,但它没有呈现任何内容。我错过了什么?

最佳答案

解决了。这与我的 ajax 调用有关。

$.ajax({
type: 'GET',
url: '/applications/generate_report?application_id='+$(this).attr('data-application-id')+'&rand_value='+$(this).attr('data-rand-value'),
dataType: 'json',
success: function(data) {
object_selected.removeAttr('disabled');
}
})

应该是这个。通过不包含 datatype ='script' 和正确的格式,即使我的 js.erb 被调用,它也没有被执行。

$.ajax({
type: 'GET',
url: '/applications/generate_report?application_id='+$(this).attr('data-application-id')+'&rand_value='+$(this).attr('data-rand-value'),
data: {
format: 'js'
},
dataType: 'script',
success: function(data) {
object_selected.removeAttr('disabled');
}
})

关于javascript - Rails js.erb 不渲染部分文件,但获取渲染的响应文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46618190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com