-
Notifications
You must be signed in to change notification settings - Fork 416
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
Using asset_path in component previews #1093
Comments
@joelhawksley nope, I still have the issue on The image path is still wrong though: <img src="/images/image.png"> instead of: <img src="/assets/image-80b04a6277839296f66f0e7de1fb03a3061322fad2273b18a375bf2f5381c41f.png"> |
TL;DR class ExampleComponentPreview < ViewComponent::Preview
include Sprockets::Rails::Helper
self.debug_assets = Rails.application.config.assets.debug
self.digest_assets = Rails.application.config.assets.digest
self.assets_prefix = Rails.application.config.assets.prefix
self.assets_precompile = Rails.application.config.assets.precompile
self.assets_environment = Rails.application.assets
self.assets_manifest = Rails.application.assets_manifest
self.resolve_assets_with = Rails.application.config.assets.resolve_with
self.check_precompiled_asset = Rails.application.config.assets.check_precompiled_asset
self.unknown_asset_fallback = Rails.application.config.assets.unknown_asset_fallback
# Expose the Rails.application precompiled asset check to the view
self.precompiled_asset_checker = -> logical_path { Rails.application.asset_precompiled? logical_path }
...
end This will set up the context in your Preview class the same way sprockets-rails currently sets up the context in Disclaimer: I do not have the deepest understanding of the inner workings of Rails and even less of an understanding of Sprockets. These are observations I made from reading the source code, so expect parts of my interpretations to be incorrect.
By default these path resolving methods are actually aliases, which is why sprockets-rails has its own In my opinion, the dynamic nature of the |
Please don't disallow view helpers in previews — I need to use them, and I suspect I'm not the only one. See #1451 for more detail on what I'm doing (though allowing component tests to do |
I got bitten by this again today. @joelhawksley how do you want to tackle this? |
@Spone I think the first step we should take is to see if these same helpers work in ActionMailer previews, and if so, how they work there. Would you be interested in taking a look? |
I guess the relevant part of ActionMailer is here: https://github.com/rails/rails/blob/main/actionmailer/lib/action_mailer/railtie.rb#L38-L40 |
@Spone would you like to take a crack at seeing if that works for us? |
Just spent a few hours banging my head against the wall because we used 🤦 |
Why? Is it fixed? 🧐 |
@reeganviljoen @Spone is right -- this isn't really fixed. I got to this issue because what I was doing was related to
Perhaps my comment should drive a separate ticket / PR that updates the documentation around testing view components... even if you're using render_inline, IF you're using any view helpers that rely on the assets pipeline (like |
@gap777 personally when ever I run tests in ci I precomile the assets as a setup step, maybe we should state that in docs |
@reeganviljoen We have 2 parallel workflows in our CI: those requiring the assets, and those that do not (unit tests, static code analysis/rubocop, etc). My mistake was that I had tried to reduce the size of the one and move these |
Steps to reproduce
image.png
inapp/assets/images/image.png
image
argument, with a preview filerender ExampleComponent.new(image: image_path("image.png")
NoMethodError
include ActionView::Helpers::AssetUrlHelper
at the top of the preview classExpected 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
The text was updated successfully, but these errors were encountered: