开发者

Rails; save a rendered views html content to file

开发者 https://www.devze.com 2023-01-01 20:23 出处:网络
I\'m trying to create a view with a download开发者_Go百科 link to download the html source?@Peter \'s solution worked for me.Here is a code sample:

I'm trying to create a view with a download开发者_Go百科 link to download the html source?


@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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消