Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssetTagHelper::image_path outside views

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?

like image 846
knoopx Avatar asked Sep 27 '10 13:09

knoopx


2 Answers

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"
like image 178
keithepley Avatar answered Jan 17 '23 00:01

keithepley


For Rails 3 please checkout the much cleaner solution here How can I use image_path inside Rails 3 Controller

like image 39
Jhony Fung Avatar answered Jan 16 '23 23:01

Jhony Fung