用户可以通过链接下载报告,链接旁边有一个文本标志,指示是否已经下载文件 - “未读'警惕。我哈瓦的部分,显示在报告索引中的单个项目:Ajax调用 - 发送数据并在单个动作中渲染部分 - 两种渲染方法
<p id="chapter_report_index_item">
<%= link_to "#{report.chapter_report_original_filename}", chapter_report_path(report), remote: true %>
<%unless check_read_status?(report) %>Unread
<%end%>
<p>
在chapter_reports_controller.rb我有 -
def show
set_chapter_report
set_read_status_true(@chapter_report)
send_data @chapter_report.chapter_report.read, filename: @chapter_report.chapter_report_original_filename
respond_to do |format|
format.js
end
end
而且在show.js,调用重新渲染此单项目部分 -
$("#chapter_report_index_item").html("<%= j (render(partial: 'index_item', locals: {report: @chapter_report})) %>");
你会注意到在视图中check_read_status
方法 - 本质上是,部分显示时,显示的想法未读标志,如果用户没有点击在下载链接上。
我的问题是,我试图在动作中渲染两件事:下载和部分。什么是避免这种技术?
本质上,我想要一个下载按钮,该按钮在下载文件时发生变化。
更新
我试图解决这个问题,虽然一个javascript给助手(按下面的评论)。我的问题是send_file
和它的同类是不能作为application_helper.rb
基本上我已经有了一个jQuery调用部分刚刚包含的下载方法的调用方法 -
<%= report.class.name %>
<%= report.chapter_report_original_filename %>
<%= asset_download(report) %>
在助手 -
def asset_download(object)
object.chapter_report_original_filename
#send_file(object.chapter_report.read, filename:object.chapter_report_original_filename)
end
我已经把电话给.class.name
等,看看有什么可以通过。当我取消了send_file
电话,我得到的错误 -
ActionView::Template::Error (undefined method `send_file' for #<#<Class:0x0...
我猜COS的方法是提供给ActionController的不的ActionView。
所以问题是 - 我如何使控制器方法在这种情况下可用?或者我找到另一种方法来做到这一点?
更新2根据以下答案,您可以通过呈现包含通过javascript调用的部分来调用send_file方法。您需要按照this question的方法在助手中提供该方法。
嗨,谢谢你。它确实发生在我尝试从js.erb文件中触发动作,但当我尝试在应用程序助手中调用某个方法时,出现“无法识别的方法”错误。你知道这个方法吗? – RADan 2014-12-08 17:14:57
当你得到'无法识别的方法'错误时,你到底在做什么? – 2014-12-09 20:27:22
我只是试图从js文件中调用erb的方法 - 例如<%j download_item(@object)%> - 其中download_item是应用程序助手中的方法。我现在正在尝试一种稍微不同的方式 - 我要更新问题。 – RADan 2014-12-10 16:48:37