Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/58.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/5/ruby/20.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 Rails下载http响应/显示站点_Ruby On Rails_Ruby_Download_Httpresponse_Sendfile - Fatal编程技术网

Ruby on rails Rails下载http响应/显示站点

Ruby on rails Rails下载http响应/显示站点,ruby-on-rails,ruby,download,httpresponse,sendfile,Ruby On Rails,Ruby,Download,Httpresponse,Sendfile,我不想在浏览器中显示index.api.rsb文件呈现的xml文件,而是想下载它。对我来说这听起来很简单,但我找不到解决办法 我在控制器方法中尝试了以下操作: def split if params[:export] == "yes" send_file *here comes the path to xml view*, :filename => "filename", :type => :xml end respond_to ... end 结果

我不想在浏览器中显示index.api.rsb文件呈现的xml文件,而是想下载它。对我来说这听起来很简单,但我找不到解决办法

我在控制器方法中尝试了以下操作:

def split
   if params[:export] == "yes"
       send_file *here comes the path to xml view*, :filename => "filename", :type => :xml
   end
   respond_to ...
end
结果是出现MissingFile异常


提前感谢

要强制下载,请将
:disposition=>附件
添加到发送文件方法中


来源:

注意,
:disposition
默认为“附件”,所以这应该不是问题

如果出现MissingFile异常,则表示路径不正确
send_file
需要实际文件的路径,而不是需要渲染的视图

对你来说,这可能是你需要的。提到它呈现视图并返回字符串,而不是设置响应主体

def split
  if params[:export] == "yes"
    send_data(render_to_string path_to_view, filename: "object.xml", type: :xml)
  end
end

仍然有一个MissingFile异常:(那么,正如Jim所说,您的文件路径可能不正确。好的,但是有下载渲染视图的选项吗?