Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't draw templates that require instance variables #555

Closed
vividmuimui opened this issue Nov 22, 2023 · 5 comments
Closed

Can't draw templates that require instance variables #555

vividmuimui opened this issue Nov 22, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@vividmuimui
Copy link

Describe the bug

When rendering view templates that require instance variables, the following error occurs

ActionView::Template::Error
'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path.

To Reproduce

Steps to reproduce the behavior:

  1. bin/rails generate scaffold Article title:string body:text
# app/views/articles/index.html.erb
<div class="w-full">
  <% if notice.present? %>
    <p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
  <% end %>

  <div class="flex justify-between items-center">
    <h1 class="font-bold text-4xl">Articles</h1>
    <%= link_to "New article", new_article_path, class: "rounded-lg py-3 px-5 bg-blue-600 text-white block font-medium" %>
  </div>

  <div id="articles" class="min-w-full">
    <%= render @articles %>
  </div>
</div>
# test/components/previews/articles/index_preview.rb
class Articles::IndexPreview < Lookbook::Preview
  def default
    render template: 'articles/index', assigns: { articles: [] }
  end
end
  1. See error http://localhost:3000/lookbook/inspect/articles/index/default

Expected behavior

I want the preview to be displayed without error.

Screenshots

image

Version numbers

Please complete the following information:

  • Lookbook: 2.1.1
  • ViewComponent: -
  • Rails: 7.1.2
  • Ruby: 3.2.1p31

Additional context

The following is a sample project that is actually in error.
https://github.com/vividmuimui/LookBookSample/tree/report-issue
https://github.com/vividmuimui/LookBookSample/blob/report-issue/test/components/previews/articles/index_preview.rb

@vividmuimui vividmuimui added the bug Something isn't working label Nov 22, 2023
@allmarkedup
Copy link
Collaborator

Hey @vividmuimui, many thanks for the detailed bug report.

I think I might know why this is not working - I'll take a look now and see if I can get it fixed 👍

@allmarkedup
Copy link
Collaborator

@vividmuimui I've just opened PR #558 that should fix the issue - can you give it a try and let me know if it works for you?

You can pull the Lookbook gem directly from the PR with the following:

gem "lookbook", github: "ViewComponent/lookbook", branch: "fix-template-assigns"

@allmarkedup
Copy link
Collaborator

Hey again @vividmuimui, just to let you know I've just released v2.2.0 that should fix the issue you were seeing. I'm going to go ahead and close this issue now but if you are still having problems with the new version feel free to reopen or let me know. Thanks again for the bug report :)

@vividmuimui
Copy link
Author

@allmarkedup
Thanks for the very quick fix!
I was able to confirm that it works fine in v2.2.0.

@allmarkedup
Copy link
Collaborator

Fantastic, thanks for letting me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants