简体   繁体   English

是否可以在模型中使用rails image_tag?

[英]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. 我有一个rails 3.x应用程序,它具有模型类,可以将内容返回到部分要在页面上呈现的视图。 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; 该模型将其内容HTML-Safe传递给视图; 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). 我需要创建一个类,它返回html以获取资源管道中图像的链接(视图不知道或不关心返回的东西是图像)。

In it's simplest form, the model reads a record from the database which contains a filename, "my_image.png". 在最简单的形式中,模型从数据库中读取包含文件名“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". 它需要返回HTML视图,该HTML引用具有该名称的图像,该图像位于“app / assets / images”中。

Is it possible to do this ? 是否有可能做到这一点 ?

这将在Model内部工作

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

尝试在模型中include ActionView::HelpersActionView::Helpers::AssetTagHelper

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM