Skip to content

Commit

Permalink
docs: use dig in layouts guide ERB
Browse files Browse the repository at this point in the history
If one uses the suggested layout for the ViewComponent default layout,
navigating to `localhost:3000/rails/view_components` will throw an error
due to the deeply nested nature of the Lookbook params hash. Using `dig`
allows for safe access into the hashes without causing problems if any
of the keys don't exist.

Hash#dig was introduced in Ruby 2.3, so will be available in all
supported versions of Lookbook.
  • Loading branch information
neanias committed Jun 24, 2024
1 parent cdcbbae commit 0e192ca
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions docs/src/_guide/previews/layouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ title: Preview Layouts
```erb
<!-- app/views/layouts/component_preview.html.erb -->
<!DOCTYPE html>
<html style="background-color: <%%= params[:lookbook][:display][:bg_color] || "white" %>">
<html style="background-color: <%%= params.dig(:lookbook, :display, :bg_color) || "white" %>">
<head>
<title>Component Preview</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
Expand All @@ -67,7 +67,7 @@ title: Preview Layouts
<div style="
margin-left: auto;
margin-right: auto;
max-width: <%%= params[:lookbook][:display][:max_width] || "100%" %>
max-width: <%%= params.dig(:lookbook, :display, :max_width) || "100%" %>
">
<%%= yield %> <!-- rendered preview will be injected here -->
</div>
Expand All @@ -86,4 +86,4 @@ title: Preview Layouts

<% s.with_block_api_module_methods(:preview_helper) %>

<% end %>
<% end %>

0 comments on commit 0e192ca

Please sign in to comment.