Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use image_url or image_path in an observer class

I want to do some Facebook posting from an observer class, however, I want to reference an image from my server. In the past I have had some pretty hackie methods of doing this, but now with the new assets stuff, I though I could use image_path or image_url methods.

However, I can't seem to figure out how to call these. They are not class level methods of ActionView::Helpers::AssetTagHelper so doing

ActionView::Helpers::AssetTagHelper.image_url

Doesn't work.

like image 902
Bill Leeper Avatar asked Oct 30 '11 22:10

Bill Leeper


2 Answers

First of all, I don't think image_url exists. I only see image_path in the API.

To be able to use that method, you have to include the AssetTagHelper module:

include ActionView::Helpers::AssetTagHelper
image_path('my_image.png')

In Rails 4 include AssetUrlHelper:

include ActionView::Helpers::AssetUrlHelper
image_path('my_image.png')
like image 82
Mischa Avatar answered Oct 21 '22 08:10

Mischa


In Rails 4 we can use image_url with host attribute.

As @piotrkaczmarek mentioned above we need to include AssetUrlHelper.

include ActionView::Helpers::AssetUrlHelper
image_url('image.jpg', host: 'http://example.com')
like image 21
Zakhar Day Avatar answered Oct 21 '22 08:10

Zakhar Day