user2225112
user2225112

Reputation: 77

Rails image from model

Hi could somebody help me out, I'm doing a rails project and am only starting out so not sure what to do. I am trying to display an image on an index page in the public folder, I also have an image folder inside the public folder. In my model I have t.string :image and in the entry itself I have the link img/cup2.jpg. Could anybody tell me if i'm missing a step somewhere? Should the image not just automatically load if that link is correct? Thanks for your help.

I am also using json to index the posts I have a bit of javascript like this to write the data in

$.get("/shops/" + i + ".json", function(json) {

    html  = "<h3>" + json.name + "</h3>";
    html += "<p>" + json.image + "</p>";
    html += "<p>" + json.address + "</p>";
    html += "<p>" + json.website + "</p>";
    html += "<p>" + json.phone + "</p>";
    html += "<p>" + json.openingHours + "</p>";
    html += "<p>Email:" + json.email + "</p>";

    $('#shopDetails').html(html)
  });

Upvotes: 0

Views: 113

Answers (1)

Robin Fisher
Robin Fisher

Reputation: 1454

The problem with your code (assuming the javascript works and the folder names are correct) is that you are just writing the URL of the image out to a <p> tag. You need to write it out into an image tag as:

"<img src='" + json.image + "' alt='description of my image' />"

As a completely separate aside, what version of Rails are you using? As of Rails 3.2, images are meant to be served out of the assets directory in your app directory.

Upvotes: 1

Related Questions