Supposedly, ActionController::Base.helpers
acts like a proxy for accessing helpers outside views. However many of the methods defined there rely on controller variables and I'm unable to succesfully call:
ActionController::Base.helpers.image_path("my_image.png")
>> TypeError Exception: can't convert nil into String
Digging at the source I see compute_asset_host
method is trying to access config.asset_host
but config
is nil
.
How can I successfully call image_path
from outside views?
Use view_context
to access those helper methods that are available in the view.
You can call image_path
like this from the controller.
view_context.image_path "my_image.png"
For Rails 3 please checkout the much cleaner solution here How can I use image_path inside Rails 3 Controller
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