Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use rails image_tag from inside a model?

I have a rails 3.x application that has model classes which return content to a view partial to be rendered on a page. The model takes care of what the content is and just returns it to the view which blindly adds it to the page. In other words, the view calls the model asking for its content; the model delivers its content HTML-Safe to the view; and the view just dumps what is returned into into its output.

Thus far, everything works.

I need to make such a class that returns the html for a link to an image in the asset pipeline (the view does not know or care that the thing being returned is an image).

In it's simplest form, the model reads a record from the database which contains a filename, "my_image.png". It needs to return to the view the HTML referring to the image with that name that is located in "app/assets/images".

Is it possible to do this ?

like image 901
starfry Avatar asked Aug 24 '12 12:08

starfry


2 Answers

This will work inside Model

ActionController::Base.helpers.image_tag("image.png")
like image 191
PradeepKumar Avatar answered Nov 06 '22 17:11

PradeepKumar


try including helper in model include ActionView::Helpers & ActionView::Helpers::AssetTagHelper

like image 44
Amar Avatar answered Nov 06 '22 17:11

Amar