Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 3 未定义的方法`发送数据';对于main:Object_Ruby On Rails 3_Amazon S3_Paperclip_Download - Fatal编程技术网

Ruby on rails 3 未定义的方法`发送数据';对于main:Object

Ruby on rails 3 未定义的方法`发送数据';对于main:Object,ruby-on-rails-3,amazon-s3,paperclip,download,Ruby On Rails 3,Amazon S3,Paperclip,Download,我正在尝试使用回形针和aws sdk gem下载存储在amazon s3 bucket中的数据。对象是可公开访问的 我的控制器中有一个方法,我打算直接下载数据 def method_where_downloading_happens code for finding url from params send_file "ModelName.attached_file.url" end #尝试了静态和动态url。 我得到以下错误 NoMethodError: undefined meth

我正在尝试使用回形针和aws sdk gem下载存储在amazon s3 bucket中的数据。对象是可公开访问的

我的控制器中有一个方法,我打算直接下载数据

def method_where_downloading_happens
  code for finding url from params
  send_file "ModelName.attached_file.url"
end
#尝试了静态和动态url。

我得到以下错误

NoMethodError: undefined method `send_file for main:Object
但是,如果我使用:-

send_file open(ModelName.attached_file.url)
然后下载文件,但其格式为二进制,即windows无法识别文件类型

请帮助我如何在用户点击链接时提供直接下载。

您可以尝试以下方法:

#In your environment.rb require 'open-uri'
require 'open-uri'

#As send_data will work in controller, so in your controller
def index
  data = open(params[:file])
  send_data data, :filename => params[:name], ...
end

您使用哪个rails版本?当
ActionController::Streaming
中的
send_data
被移动到Rails v3.0.9中的
AC::DataStreaming
|您正在编写下载文件的代码时?@Kers我正在使用Rails 3.2.9和ruby 1.9.3,@RAJ…我写这篇文章的方法是希望用户来下载链接。
require'open uri'
在环境中添加后仍然返回
false
。如果您从控制台运行
require'open uri'
,并返回
false
,这意味着
'open-uri'
已经包含在应用程序中。所以,不用担心