Rails 3.1 Having iframe in view makes layout stop

2020-07-17 14:25发布

问题:

So I have a basic layout file:

<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
  <%= stylesheet_link_tag    "logged_out" %>
  <%= javascript_include_tag "application" %>
  <%= stylesheet_link_tag "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/ui-lightness/jquery-ui.css" %>
</head>
<body>
  <!-- header stuff here -->
  <%= yield %>
  <!-- footer stuff here -->
</body>
</html>

And with any normal html its fine. However if I add in an iframe like this to a view:

<iframe id="form" height="480" width="320" src="/mobile_preview/preview"/>

When I render the page everything is rendered up until the iframe, but the footer stuff after yield doesn't render. Has anyone run into this before?

EDIT: As one of the answers pointed out (thank you!), my yield statement in my initial question was wrong. My yield statement in my code is correct though, it was a typo when transferring to stackoverflow.

NOTE: If you are trying to replicate the iframe is using jquery mobile.

回答1:

The problem is how you include <iframe>. You think you included self-closing tag, and it ends there. But you don't send your page as XML, and HTML doesn't have concept of self-closing tags, it's just garbage at the end. So your:

<iframe id="form" height="480" width="320" src="/mobile_preview/preview"/>

is really interpreted as:

<iframe id="form" height="480" width="320" src="/mobile_preview/preview">

and rest of the page is interpreted as ignored content inside <iframe> tag. That's why you shouldn't use self-closing tags in HTML document - they don't really work the way you think they work.

Change it to:

<iframe id="form" height="480" width="320" src="/mobile_preview/preview"></iframe>

You could found it if you looked at parsed DOM tree with Firebug or Chrome Inspector.

As a bonus: it has nothing to do with Rails, server returns response just as previously, you could see it in logs. It's just a matter how your markup is interpreted by browsers.



回答2:

You have wrong on clossing ruby code place

<%= yield =>

The correct is

<%= yield %>