I got an constants.rb file in config/initializers with the following content:
DEFAULT_IMAGES = {
profile: ActionController::Base.helpers.image_url('v2/default_profilepic.jpg'),
banner: ActionController::Base.helpers.image_url('v2/profile-banner.jpg'),
missing: ActionController::Base.helpers.image_url('v2/missing.png'),
}
When I try to call DEFAULT_IMAGES somewhere in the code or even in the console then I get the images back without the hash. Is it that what its supposed to do and am I wrong in my expectations?
DEFAULT_IMAGES => {:profile=>"http://localhost:3000/assets/v2/default_profilepic.jpg", :banner=>"http://localhost:3000/assets/v2/profile-banner.jpg", :missing=>"http://localhost:3000/assets/v2/missing.png"}
I would expect something like this "/assets/v2/missing-d38d4bdbf9f2cf313e346a844de298c0.png"
You could also give Proc a try
proc { ActionController::Base.helpers.image_path('v2/missing.png') }.call
Put your code into an after_initialize block within your specific environment file or in application.rb:
config.after_initialize do
DEFAULT_IMAGES = {
profile: ActionController::Base.helpers.image_url('v2/default_profilepic.jpg'),
banner: ActionController::Base.helpers.image_url('v2/profile-banner.jpg'),
missing: ActionController::Base.helpers.image_url('v2/missing.png'),
}
end
Drawback is that you'll need to reference your constant through the namespace of your application:
Foo::Application::DEFAULT_IMAGES
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With