How to add your images to Rails and make Heroku host them!

First, upload your images to app/assets/images and push to heroku.
Heroku needs Rails to 'precompile' your images in order to serve them. Run heroku run rake assets:precompile at your command prompt. In your views, you will need to reference the public image path, and Rails fills this path in for you using two functions:
image_tag filename, height: height, width: width, class: class, alt: alt text
(filename being the only required parameter, with just the image name and extension)
This function creates a complete tag for you.

You can use the following function if you need just the image's path & filename (i.e. background attributes and CSS)
image_path filename (once again, filename is just the file name and extension)

Happy coding!

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.