【问题标题】:Rails; save a rendered views html content to file导轨;将呈现的视图 html 内容保存到文件
【发布时间】:2011-07-05 10:08:15
【问题描述】:

我正在尝试创建一个带有下载链接的视图以下载 html 源代码?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

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

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

    控制器:

    默认显示
      # ...
      如果参数[:下载]
        send_data(render_to_string, :filename => "object.html", :type => "text/html")
      别的
        # 正常渲染
      结尾
    结尾
    

    【讨论】:

      【解决方案2】:

      您可以使用render_to_string 代替渲染,这将为您提供页面,然后使用send_data 下载它。

      More on render to string heremore on send_data here

      【讨论】:

      • 谢谢,我在其他搜索中看到过,但使用时遇到问题。你有例子吗?
      猜你喜欢
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 2011-11-05
      • 2020-06-15
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-17
      相关资源
      最近更新 更多