在Rails4中下载公共文件

时间:2014-01-22 13:59:31

标签: ruby-on-rails ruby nginx download

我正在使用Rails-4.0.2,ruby 2.1.0并且有几个公共文件,它们位于我的app / public文件夹中。在index.html视图中,我有以下表格供下载。

<table class="table table-condensed table-striped" style="border-collapse: collapse;
      border-spacing: 0; width: 50%;">
  <tbody>
    <% @reports.each do |report| %>
      <tr>
        <td><%= report.title %></td>
        <td> <a href=<%= "#{report.url}"%>> Download </a></td>
      </tr>
    <% end %>
  </tbody>

在HTML代码中,我有以下链接:

<a href="reports/2014-01-22-13:45:13-UTC.xlsx">Download</a>

当我在127.0.0.1:3000开发阶段点击此链接时,浏览器正在下载此文件。这就是我需要的。

但是当我在生产阶段点击同一地址127.0.0.1:3000时,我遇到以下错误:

Started GET "/reports/2014-01-22-13:45:13-UTC.xlsx" for 127.0.0.1 at 2014-01-22 17:50:13 +0400
Processing by ReportsController#show as XLSX
 Parameters: {"id"=>"2014-01-22-13:45:13-UTC"}
 Completed 404 Not Found in 2ms

 ActiveRecord::RecordNotFound (Couldn't find Report with 
 id=2014-01-22-13:45:13-UTC):
 app/controllers/reports_controller.rb:60:in `set_report'

这只是公共文件夹中的静态文件,如何在生产中下载?

这是我的配置文件:

发展:

GetLead::Application.configure do
  config.cache_classes = false
  config.eager_load = true
  config.serve_static_assets = true

  config.assets.initialize_on_precompile = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = false
  config.assets.compress = true
  config.assets.compile = true

  config.assets.digest = true
  config.log_level = :info
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
  config.log_formatter = ::Logger::Formatter.new
end

生产:

GetLead::Application.configure do
  config.cache_classes = true
  config.eager_load = true
  config.consider_all_requests_local       = false
  config.action_controller.perform_caching = true
  config.serve_static_assets = false
  config.assets.compress = true
  config.assets.compile = true
  config.assets.digest = true
  config.i18n.fallbacks = true
  config.active_support.deprecation = :notify
end

提前致谢!

1 个答案:

答案 0 :(得分:1)

文件不会在生产中传递,因为

# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = false

我建议您使用真实的网络服务器(如nginx或Apache)来提供静态文件! (作为配置nginx的起点,请参阅http://wiki.nginx.org/RubyonRailsMongrel或Google)

相关问题