ViewComponent / demo

Rails application with ViewComponent
MIT License
157 stars 15 forks source link

ActionView::Template::Error (uninitialized constant Box) when visiting `/benchmark` #52

Closed stefcoetzee closed 3 years ago

stefcoetzee commented 3 years ago

Steps to reproduce

Run bin/rails server and visit localhost:3000/benchmark

Expected behavior

Show view containing a series of nested boxes and terminal report stating how many times faster ViewComponent is compared to partial.

Actual behavior

Raises error "ActionView::Template::Error (uninitialized constant Box)" on line 3 of app/views/demo/benchmark.html.erb.

System configuration

Rails version: 6.1.0

Ruby version: 3.0.0p0

Gem version: 2.25.0


Why does it happen?

This is my first time logging an issue, but I do think that render Box should be changed to render(BoxComponent.new), as is used in RailsConf 2020 CE - Encapsulating Views by Joel Hawksley.

Fixing it

Replace all instances of render Box inside app/views/demo/benchmark.html.erb with render(BoxComponent.new), as shown:

# app/views/demo/benchmark.html.erb

<%= render(BoxComponent.new) do %>
  Inside a box
  <%= render(BoxComponent.new) do %>
    Inside a box
    <%= render(BoxComponent.new) do %>
      Inside a box
      <%= render(BoxComponent.new) do %>
        Inside a box
        <%= render(BoxComponent.new) do %>
          Inside a box
          <%= render(BoxComponent.new) do %>
            Inside a box
            <%= render(BoxComponent.new) do %>
              Inside a box
              <%= render(BoxComponent.new) do %>
                Inside a box
                <%= render(BoxComponent.new) do %>
                  Inside a box
                  <%= render(BoxComponent.new) do %>
                    Inside a box
                  <% end %>
                <% end %>
              <% end %>
            <% end %>
          <% end %>
        <% end %>
      <% end %>
    <% end %>
  <% end %>
<% end %>

If this is not helpful please let me know how it can be improved. Thank you for the gem and for sharing so much so openly.

joelhawksley commented 3 years ago

@stefcoetzee thanks for filing this issue! Your fix looks correct. Mind opening a PR?

joelhawksley commented 3 years ago

Closed by https://github.com/joelhawksley/view-component-demo/pull/53 ❤️