view_component: undefined local variable or method `preview_source'

The preview.erb.html rendered by the ViewComponent Previews uses a PreviewHelper method.

In my app, which has 1 preview, this method was not found.

Are you using the Source feature in Previews?

Steps to reproduce

I did this:

  • In config/environments/development.rb, config.view_component.show_previews_source = true
  • In config/application.rb config.view_component.preview_paths << "#{Rails.root}/spec/component_previews"
  • Added a preview to spec/component_previews/admin/attributes_table_component_preview.rb
    class Admin::AttributesTableComponentPreview < ViewComponent::Preview
      def with_an_empty_list
        render(Admin::AttributesTableComponent.new(data: []))
      end
    end
    

When rendering, I got the error described below.

Expected behavior

The component to render, and after it, a source code display with syntax highlighting.

Actual behavior

 Showing /home/olle/code/myapp/gems/gems/view_component-2.82.0/app/views/view_components/preview.html.erb where line #12 raised:

undefined local variable or method `preview_source' for #<ActionView::Base:0x00000000097c70>

Workaround: inlined the check for whether to render “preview_source” or not:

In using the latest version, I could see that it was doable except for finding the helper method - I inlined the check for @render_args which is perhaps an alright change to this very literal view file?

<% if ViewComponent::Base.config.show_previews_source %>
  <% unless @render_args.nil? %>
    <%= render "preview_source" %>
  <% end %>
<% end %>

I created in my application code, this file: app/views/view_components/preview.html.erb


<% # Line 13: The PreviewHelper seems not to be available, so we have inlined it. Workaround from https://github.com/ViewComponent/view_component/issues/1654 %>
<% if @render_args[:component] %>
  <% if ViewComponent::Base.config.render_monkey_patch_enabled || Rails.version.to_f >= 6.1 %>
    <%= render(@render_args[:component], @render_args[:args], &@render_args[:block]) %>
  <% else %>
    <%= render_component(@render_args[:component], &@render_args[:block]) %>
  <% end %>
<% else %>
  <%= render template: @render_args[:template], locals: @render_args[:locals] || {} %>
<% end %>

<% if ViewComponent::Base.config.show_previews_source %>
  <%# <%= preview_source %1> %>
  <% unless @render_args.nil? %>
    <%= render "preview_source" %>
  <% end %>
<% end %>

System configuration

Rails version: 6.1

Ruby version: 3.1.3

Gem version: 2.82.0

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (12 by maintainers)

Commits related to this issue

Most upvoted comments

The cause of this error is that previews are rendered via ViewComponentsController by default, and that controller does not include the PreviewHelper that defines preview_source.

A workaround for me is to define a custom controller that does include the helper:

# config/application.rb

config.view_component.show_previews_source = true
config.view_component.preview_controller = "ComponentPreviewController"

# app/controllers/component_preview_controller.rb
class Test::ComponentPreviewController < ViewComponentsController
  helper :preview
end

I could not reproduce the original issue. With Rails 6.0 and view_component-3.0.0 rendering the preview (/rails/view_components/admin/attributes_table_component/with_an_empty_list) worked fine for me.

Let’s see what I can do tomorrow,

Cc @olleolleolle