13

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

aarona
  • 35,986
  • 41
  • 138
  • 186
user354250
  • 131
  • 1
  • 3

2 Answers2

16

@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
muirbot
  • 2,061
  • 1
  • 20
  • 29
7

You can use render_to_string instead of render, which will give you the page, then to download it use send_data.

More on render to string here, and more on send_data here.

Peter
  • 127,331
  • 53
  • 180
  • 211