Prerequisites: run component setup (view_primitives:install) once per app.
Accessible <iframe> with sandboxing, lazy loading, and optional aspect-ratio wrapper.
rails g view_primitives:add iframeCreates app/components/ui/iframe_component.rb.
Refresh after a gem upgrade:
rails g view_primitives:update --only iframe<%= ui :iframe, src: "https://www.youtube.com/embed/dQw4w9WgXcQ",
title: "YouTube video: Rick Astley — Never Gonna Give You Up",
aspect: "16/9" %>Pass aspect: to wrap the iframe in a <div> with aspect-ratio inline style. This avoids the need for explicit width and height values.
<%= ui :iframe, src: "https://player.vimeo.com/video/123456",
title: "Product demo video",
aspect: "4/3" %><%= ui :iframe, src: "https://maps.google.com/...",
title: "Store location map",
width: 600, height: 450 %>The default sandbox permits scripts, same-origin, forms, and popups. Supply a string to restrict further, or pass sandbox: false to disable sandboxing (not recommended):
<%# Stricter sandbox — scripts only %>
<%= ui :iframe, src: "...", title: "Widget",
sandbox: "allow-scripts" %>| Option | Type | Default | Description |
|---|---|---|---|
src |
String | required | URL to embed |
title |
String | required | Accessible description of the embedded content |
loading |
Symbol | :lazy |
:lazy or :eager |
sandbox |
Boolean or String | true |
true = default safe tokens; string = custom tokens; false = no sandbox |
aspect |
String | nil |
CSS aspect-ratio value — wraps iframe in a <div> when present |
width |
Integer | nil |
Explicit pixel width |
height |
Integer | nil |
Explicit pixel height |
**html_attrs |
Hash | — | Forwarded to the <iframe> element |