【问题标题】:Trying to download files from a rails application尝试从 Rails 应用程序下载文件
【发布时间】:2014-09-08 07:18:54
【问题描述】:

我在从 Rails 应用程序下载文件时遇到问题。如果有人能给我一些关于我错过了什么和做错了什么的见解,我将不胜感激。另外,我一直在阅读send_datasend_file。当要求为send_data 发送数据时,我不明白这意味着什么,而为send_file 发送路径。有什么不同?再次感谢!

routes.rb

    resources :companies do
     member do
       get 'send_document'
     end
    end

查看代码(在 static_pages_controller 中,而不是 Companies_controller 中)

     <ul>
    <% @companies.each do |company| %>
        <li>
            <%= company.compName %>
            <%= company.formType %>
            <%= company.fileLocation %>
            <%= link_to "View Document", controller: :companies,             send_document_path(company), , method: :get %>
        </li>
    <% end %>
</ul>

companies_controller.rb

def send_document
 @company = Company.find(params[:id])
 send_file "#{@company.fileLocation}"
end

错误:我在 link_to 行收到错误。我在尝试下载数据库中特定公司的文件时遇到问题。任何帮助表示赞赏。谢谢!此外,在 Companies_controller 中,代码 Company.find(params[:id]) 表示它没有公司 ID。

更新:我将代码更改为

    <%= link_to "View Document", send_document_path(company), {controller: :companies}, method: :get %>

现在收到此错误:

app/views/layouts/_search_bar.html.erb where line #6 raised:

undefined method `send_document_path' for #<#<Class:0x00000005446998>:0x00000004147360>

Extracted source (around line #6):

3
4
5
6<%= link_to "View Document", send_document_path(company), {controller: :companies},   method: :get %>
7
8
9

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 你能发布错误吗??
  • 嗨,加根!感谢回复。我已经用我发现的新错误更新了我的帖子

标签: ruby-on-rails


【解决方案1】:

根据您的路由文件,您的链接中指定的路径不正确,您不需要指定控制器。

<%= link_to "View Document", send_document_path(company), {controller: :companies}, method: :get %>

应该是

<%= link_to "View Document", send_document_company_path(company), method: :get %>

【讨论】:

  • 谢谢!那成功了。你知道我在哪里可以找到文档以知道要写什么路径。你怎么知道它是 send_document_company_path 而不仅仅是 send_document_path?再次感谢!
  • 在控制台中运行“rake routes”以查看所有路由。每条路线的路径将出现在第一(左)列中。在将您的路由文件设置与我的一个也将操作附加到成员的路径文件设置进行比较后,我意识到路径不正确,并注意到我的路由将模型名称附加到该操作的路径,与您的不同。
  • 就你问题的第二部分而言,它类似于this post。顺便说一句,@DariusTran 如果这被证明是您问题的解决方案,您最好将其标记为“已回答”。 :)
  • 谢谢!我以为我做到了。检查点对勾吗?
  • 是的。你说对了。很高兴听到它也有效!不客气。
【解决方案2】:
<%= link_to "View Document", controller: :companies, send_document_path(company), , method: :get %>

它不应该是,,,只是一个,

 <%= link_to "View Document", controller: :companies, send_document_path(company),  method: :get %>

【讨论】:

  • 抱歉,这只是我在将代码放到 stackoverflow 时的输入错误。这是说 ( ) 导致语法错误。不过谢谢!
  • 语法错误,意外 ',',期待 => ...s,send_document_path(company),方法::get );@output_buffe... ... ^
  • 是的,请使用我的链接_发布&lt;%= link_to "View Document", controller: :companies, send_document_path(company), method: :get %&gt;这个
  • 我使用了该代码并收到了同样的错误。
  • 嗨拉贾希。我通过在控制器周围添加 { } 来修复错误。我收到一个新错误,希望您能提供帮助。感谢之前的建议!
【解决方案3】:

send_data(data, options = {})

将给定的二进制数据发送到浏览器。此方法类似于 render plain: data,但还允许您指定浏览器是否应将响应显示为文件附件(即在下载对话框中)或内联数据。您还可以设置内容类型、外观文件名和其他内容。


send_file(path, options = {})

发送文件。这通过 Rack::Sendfile 中间件使用适合服务器的方法(例如 X-Sendfile)。要使用的标头是通过 config.action_dispatch.x_sendfile_header 设置的。您的服务器也可以通过设置 X-Sendfile-Type 标头为您配置。

More Info

编辑:

我在您的代码中发现语法错误:

<%= link_to "View Document", controller: :companies,             send_document_path(company), , method: :get %>

send_document_path(company)之后的上面一行中有两次逗号(,)可能有错误..

更新:

<%= link_to "View Document", :url => { :controller => "companies", :action => "send_document(company)" } ,:method => :get %>

【讨论】:

    猜你喜欢
    • 2011-09-17
    • 2020-07-30
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2015-01-22
    相关资源
    最近更新 更多