Rails; save a rendered views html content to file

前端 未结 2 1249
花落未央
花落未央 2020-12-28 20:55

I\'m trying to create a view with a download link to download the html source?

2条回答
  •  别那么骄傲
    2020-12-28 21:12

    @Peter 's solution worked for me. Here is a code sample:

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

    Controller:

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

提交回复
热议问题