[英]How to give the xls file link in Ruby on Rails?
How to give the xls file link in ruby. This is my file path
link_to "Excel", "/#{RAILS_ROOT}/public/reports/10014_ByNetwork.xls", :target=>"_blank"
when i given above link that is converting like this 当我给出上面这样转换的链接时
<a href="//home/kiran/shekar/wavespot/public/reports/10014_ByNetwork.xls" target="_blank">Excel</a>
So its not working. 所以它不起作用。 actually i need like this
其实我需要这样
<a href="file:///home/kiran/shekar/wavespot/public/reports/10014_ByNetwork.xls" target="_blank">Excel</a>
Please give me exact path... 请给我确切的路径...
@Gagan Your syntax is incorrect. @Gagan您的语法不正确。 You should test your answers before posting them.
在发布答案之前,您应该先对其进行测试。 This is the correct way:
这是正确的方法:
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls", target: "_blank" %>
OR 要么
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls", :target=>"_blank" %>
You have missed out the comma after the second closing double quotes. 在第二个双引号引起来之后,您错过了逗号。
Are you sure that you need file:// link? 您确定需要file://链接吗? It is not operated by rails server and will work only on your machine.
它不是由Rails服务器操作的,只能在您的机器上使用。 Maybe it will be better to generate link like this: " http://you_server_url.org/public/file.xml ".
生成这样的链接可能会更好:“ http://you_server_url.org/public/file.xml ”。 It works fine on local and remote app, and you can manage file-sending by controller action.
它在本地和远程应用程序上都可以正常工作,并且您可以通过控制器操作来管理文件发送。
在您的视图文件中尝试以下操作:
<%= link_to 'Excel',"/reports/10014_ByNetwork.xls" :target=>"_blank" %>
if you have more than one file to download and you have to use different links along your views I could recommend you the next approach too: 如果您要下载多个文件,并且必须在视图中使用不同的链接,我也可以建议您使用下一种方法:
Add a download xls get resource and route helper to your routes.rb
file like: 添加下载xls 获取资源并将路由助手添加到您的
routes.rb
文件,例如:
get "downloads/xls/:id" => "downloads#xls", :as => :download_xls
In your controller, for my my example I'll use app/controllers/downloads_controller.rb
we will need to add the xls action to stream data with send_file
: 在您的控制器中,对于我的示例,我将使用
app/controllers/downloads_controller.rb
我们将需要添加xls操作以使用send_file
流数据:
def xls
if params[:id]
send_file("#{Rails.root}/public/reports/#{params[:id]}.xls",
filename: "#{params[:id]}.xls",
type: 'application/excel',
disposition: 'attachment')
end
end
You can read more about it here: http://apidock.com/rails/ActionController/DataStreaming/send_file 您可以在此处了解更多信息: http : //apidock.com/rails/ActionController/DataStreaming/send_file
And finally in your view you'll use the link_to
helper with our declared above download_xls_path
route and the filename as param: 最后,在您看来,您将使用
link_to
帮助器以及上面声明的download_xls_path
路由,并将文件名用作参数:
<p>
Click to download: </br>
<%= link_to "NameOfYourXlsFile.xls", download_xls_path(NameOfYourXlsFile) %>
</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.