Spencer R
Spencer R

Reputation: 1158

Display stored HTML inline in Rails 3

I'm kind of stuck on a problem and I need help getting around it.

I'm developing a Rails 3 app that will allow users to interact with our email service provider via SOAP services. Users will be able to upload full HTML files which will be stored in the app's database (MySQL). I'm trying to create a view that will give the user a preview of the HTML content saved on the database.

More often than not, the HTML documents will have some type of styling applied to the background (like <body bgcolor="#e5e5e5"> or <div style="background:#e5e5e5;"> or both. That styling gets applied to the entire page. Where I'm stuck is creating a container for the inline HTML that will constrain those styles. I was thinking <iframe>, but that seems to want a src attribute, but the source of my HTML is the database. Any ideas how I would accomplish this?

Here's the view code I have:

<p>Content for: <%= @doc.file_name %> </p>

<div>
  <%= render :inline => @doc.content %>
</div>

Here's the source code of the rendered HTML page if that helps you to visualize what I'm doing:

<!DOCTYPE html>
<html>
<head>
  <title>ListTool</title>
<meta name="csrf-param" content="authenticity_token"/>
<meta name="csrf-token" content="W7AoMYkiW8gDdKYDNB/NemCY/mRz+JeNadb3pexhaIo="/>
</head>
<body>

<p>Content for: HJ4151.html </p>

<div>
  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html><head><meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">

<title>The Bankers&rsquo; Black Book of High-Yield Dividend Plays
</title>

</head>



<body marginheight="0" marginwidth="0" topmargin="0"
bgcolor="#e5e5e5">

<div style="width:100%; background:#e5e5e5; margin:0; padding:0;">

_irrelevant HTML code here_
</div>
</body>
</html>
</div>

</body>
</html>

Upvotes: 1

Views: 1397

Answers (1)

robertc
robertc

Reputation: 75717

I think the best way to get this to work is an iframe, what you're trying to do is embed a second document inside the first and that is exactly what iframe is for. The way I would approach this sort of thing is to create a page/view which emits the page from the database, so something like a template with just this in it:

<%= render :inline => @doc.content %>

The controller should get an id parameter from the URL and load the correct object. Then change your existing template to be something like this:

<p>Content for: <%= @doc.file_name %> </p>

<iframe src="/docviewer/<%= @doc.id %>">
</iframe>

I've no idea if that's valid syntax for Rails 3, but hopefully you get the idea.

Upvotes: 2

Related Questions