van2
van2

Reputation: 3

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 displays 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>

Upvotes: 0

Views: 5849

Answers (2)

tusharr
tusharr

Reputation: 59

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.

Upvotes: 5

apneadiving
apneadiving

Reputation: 115541

You should use raw to unescape html

<%=raw img_path %>

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

Upvotes: 3

Related Questions