如何在ROR的显示页面上放置下载选项

时间:2016-12-07 08:45:48

标签: ruby-on-rails

我想在我的节目页面上放置下载功能,但是当页面打开时,它不会自动下载,而不会点击任何地方,只有图像下载不起作用。 显示表格 -

<tr>
  <th><strong>Download Document</strong></th>
  <td><%= link_to '',download_screenshot_path(id: issue_request.id),class: 'btn btn-xs fa fa-download' %></td>

  <th><strong>Download Image</strong></th>
  <td><%= link_to '',download_screenshot_image_path(id: issue_request.id),class: 'btn btn-xs fa fa-picture-o' %></td>
</tr>

控制器方法 -

def show
  @issue_request = IssueRequest.find(params[:id])
  send_file @issue_request.document1.path,
            filename: @issue_request.document1_file_name,
            type: @issue_request.document1_content_type,
            disposition: 'attachment'

  @issue_request = IssueRequest.find(params[:id])
  send_file @issue_request.document2.path,
            filename: @issue_request.document2_file_name,
            type: @issue_request.document2_content_type,
            disposition: 'attachment'
end

2 个答案:

答案 0 :(得分:0)

不要在show方法中包含send_file,它会立即进行下载。你想要一个单独的行动。

我假设您已经为您的下载路径设置了路由,它们应该被设置为类似...

get '/screenshot', to: 'issue_requests#download_screenshot', as: 'download_screenshot'

然后在issue_requests_controller.rb中你会有

def download_screenshot
  issue_request = IssueRequest.find(params[:id])
  send_file issue_request.document1.path,
            filename: issue_request.document1_file_name,
            type: issue_request.document1_content_type,
            disposition: 'attachment'
end  

download_screenshot_image执行类似的操作,使用不同的控制器操作路径。

这样,只有在单击link_to时才会发生下载。

答案 1 :(得分:0)

请在link_to标签中指定文字,否则按钮不会出现 像这样

&lt;%= link_to'Test Button',download_screenshot_path(id:issue_request.id),class:'btn btn-xs fa fa-download'%&gt;