Skip to content

Using asset_path in component previews #1093

@Spone

Description

@Spone

Steps to reproduce

  • put image.png in app/assets/images/image.png
  • create an example component that accepts an image argument, with a preview file
  • in the preview file, add render ExampleComponent.new(image: image_path("image.png")
  • this will trigger a NoMethodError
  • add include ActionView::Helpers::AssetUrlHelper at the top of the preview class
class ExampleComponent < ApplicationComponent
  def initialize(image:)
    @image = image
  end

  def call
    image_tag @image
  end
end
class ExampleComponentPreview < ViewComponent::Preview
  include ActionView::Helpers::AssetUrlHelper

  def default
    render(ExampleComponent.new(image: image_path("image.png")))
  end
end

Expected behavior

The preview renders the image with path /assets/image-80b04a6277839296f66f0e7de1fb03a3061322fad2273b18a375bf2f5381c41f.png.

Actual behavior

The preview renders but the image path is /images/image.png.

System configuration

Rails version: 6.1.4.1

Ruby version: 2.7.4

Gem version: 2.40.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions