send_file link 在 Rails 上 Ruby 不工作

send_file link not working Ruby on Rails

我正在尝试下载文件而不是在浏览器中打开它们 - 文件类型可能不同 - 到目前为止我有以下设置 - 它什么都不做

观看次数

 <td><%= link_to "Download", download_file_path(resume) %></td>

在控制器中

  def download_file(file_path)
    mime = MIME::Types.type_for(file).first.content_type
    send_file(file_path, :type => mime, :disposition => "attachment")
  end

在路线中

 get 'profiles/download_file'          => 'profiles#download_file' , as: :download_file

这些设置无效,页面只是刷新 - 仅供参考:我正在使用载波作为附件

更新: 控制台中显示的日志

Started GET "/profiles/download_file.2" for 127.0.0.1 at 2016-07-18 19:25:23 +0500 Processing by ProfilesController#show as Parameters: {"id"=>"download_file"}

您的路线不允许下载记录的标识符。

get 'profiles/:id/download_file'          => 'profiles#download_file' , as: :download_file

尽管这实际上应该是您资源路由上的成员操作。

你也不想要控制器方法的参数,你需要从参数中提取 id:

def download_file
  file = Resume.find(params[:id])
  ...
end