Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionController::Base.helpers.image_url doesnt generate hashed version in initializer

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"

like image 795
ticcoid Avatar asked Jun 17 '26 08:06

ticcoid


2 Answers

You could also give Proc a try

proc { ActionController::Base.helpers.image_path('v2/missing.png') }.call
like image 101
schmierkov Avatar answered Jun 19 '26 23:06

schmierkov


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
like image 44
raikorious Avatar answered Jun 19 '26 23:06

raikorious



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!