lookbook-hq / lookbook

A UI development environment for Ruby on Rails apps ✨

Home Page:https://lookbook.build

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting missing partial error for collections on v2

robinator opened this issue · comments

Describe the bug

Whenever a partial references partials within it (relative to it), there is a missing partial error raised. I believe this is because the parent partial's directory is not in the partial search path:

To Reproduce

Given these partials:

# app/views/shifts/_shift_card.html.slim
.shifts-card
   = render shifts # an array of Shift instances

and

# app/views/shifts/_shift.html.slim
.shift
   = shift.name

Calling this in a preview will throw an error:

def shift_card
  render 'shifts/shift_card', shifts: FactoryBot.create_list(:shift, 2)
end
Missing partial lookbook/shifts/_shift with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :slim]}.

Searched in:
  * ".../test/components/previews"
  * ".../app/views"

Expected behavior

Rails collections partials should be rendered normally without including the direct path from within the partial.

Version numbers

Please complete the following information:

  • Lookbook: '>= 2.0.0-rc.2'
  • ViewComponent: > 2.0
  • Rails: 7.1.0.alpha
  • Ruby: 3.2.2

Additional context

Rendering works if the direct path from the views directory is given:

= render shifts # fails
= render partial: 'shift', collection: shifts # fails
= render partial: 'shifts/shift', collection: shifts # works

I believe we can fix if we can include the directory that the original partial is in in the partial search path.

I have found that including this line ActionView::Base.prefix_partial_path_with_controller_namespace = false fixes the issue. I believe it's because rails is including the lookbook namespace in the partial path rather than using the rails app itself. I'm not sure if this is right place for the fix but something like this worked for me:

module Lookbook
  module PreviewControllerActions
    extend ActiveSupport::Concern

    included do
      helper PreviewHelper
      helper Rails.application.routes.url_helpers
      prepend_view_path Engine.root.join("app/views")
      ActionView::Base.prefix_partial_path_with_controller_namespace = false
    end
...
end

Hi @robinator, thanks for opening the issue and for your time looking into this! I've been a bit slammed recently unfortunately but I'll try to dig in a bit and see if your fix here has any other implications as soon as I get a chance. Thank you again 🙏

Hey again @robinator. I've spent some time looking into this a bit more and it turns out it's a bit of a sticky one!

Unfortunately the ActionView::Base.prefix_partial_path_with_controller_namespace = false fix you mention doesn't seem to work consistently for me across all potential preview controller configurations, although I'm currently a bit stumped as to why not.

I also tried adding the rendered partial's parent directory into the partial search path, but of course that doesn't help if the partial path is still being prefixed with the controller name.

Whilst this is definitely a bug (Lookbook should definitely resolve partial paths identically to the main app) I'm not going to let this block the v2 release as there is a workaround (i.e. specify the full path to the partial relative to the view directory root) for now.

I'll add a note about the issue to the docs for now and revisit this as soon as I've got the v2 release out the door. Thanks for your patience!

@robinator I've just released v2.3.0 that should (finally!) fix this issue thanks to a contribution from @liram11. So I'm going to close this down now - if you upgrade and are still having problems then of course please feel free to reopen.

@allmarkedup Thanks for reaching out! I've confirmed my original issue is fixed without needing my hacks 👍🏻

@robinator fantastic, appreciate you confirming it's working now :)