Search code examples
ruby-on-railsmodel

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 ?


Solution

  • This will work inside Model or Controller

    ActionController::Base.helpers.image_tag("image.png")