开发者

Display an image from a file location

I would like to show an image from a location in my public directory. In my model I have created a path for the image but when I call it in the show action it simply displ开发者_开发百科ays the string.

  def img_path
"<img src='/system/assets/10/original/airplane.jpg'>"
  end

Here is the html:

      <td>&lt;img src='/system/assets/10/original/airplane.jpg'&gt;</td>


The best way to do it is that your model defines an image_path method

class Image < ActiveRecord::Base
  def image_path
     "/system/assets/#{id}/original/#{filename}"
  end
end

and in a helper or in the view you can do something like

<%= image_tag(image.image_path) %>

Where image is the instance of the type Image

This way you won't have to deal with escaping and unescaping issues.


You should use raw to unescape html

<%=raw img_path %>

It's part of the security measures built-in Rails

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜