轨; 将呈现的视图html内容保存到文件

use*_*250 13 ruby-on-rails

我正在尝试使用下载链接创建一个视图来下载html源代码?

mui*_*bot 16

@Peter的解决方案对我有用.这是一个代码示例:

视图:
<%= link_to 'download this page', object_path(@object, :download => true) %>

控制器:

def show
  # ...
  if params[:download]
    send_data(render_to_string, :filename => "object.html", :type => "text/html")
  else
    # render normally
  end
end


Pet*_*ter 7

您可以使用render_to_string而不是渲染,它将为您提供页面,然后下载使用send_data.

更多关于渲染到字符串这里,和更多的send_data在这里.