user354250
user354250

Reputation: 131

Rails; save a rendered views html content to file

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

Upvotes: 13

Views: 7716

Answers (2)

muirbot
muirbot

Reputation: 2081

@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

Upvotes: 16

Peter
Peter

Reputation: 132227

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.

Upvotes: 7

Related Questions