why use image_tag helper and not <img> tag

josh wrote:

I was just wondering what the advantage is to using the image_tag helper
over the standard <img> html tag?

Also the helper seems to place a load of random numbers on the end, eg
'?63565665' why does it do this and what are they for?

There are a few reasons to use image_tag over <img>. One is easy typing for a programmer. Compare:

image_tag('foo') vs. <img src="/images/foo.png" />

Which would you rather type? Another is asset timestamping comes for free. Those are those funny numbers on the end. They are the timestamp of the file. This allows you to enable HTTP caching of images forever but still allow them to expire when you update the image

A final reason is you can configure your static assets to be stored on a different server that is built to serve static resources quickly. A simple config change and all your images change from:

<img src="/images/foo.png?63565665" />
to
<img src="http://assets.yourcompany.com/images/foo.png?63565665" />

You don't have to change all that code manually. Even if you choose to serve the static assets from the same server as dynamic requests you might still want to do this because the popular browsers limit the number of connections to each host to three requests. Setting up different virtual hosts (even if they are actually the same server) allows you to bypass this limit so your page can be loaded quicker.

You get all that and more from using image_tag instead of <img>.

Eric

Eric Anderson wrote:

josh wrote:
> I was just wondering what the advantage is to using the image_tag helper
> over the standard <img> html tag?
>
> Also the helper seems to place a load of random numbers on the end, eg
> '?63565665' why does it do this and what are they for?

There are a few reasons to use image_tag over <img>. One is easy typing
for a programmer. Compare:

image_tag('foo') vs. <img src="/images/foo.png" />

[snip]

You get all that and more from using image_tag instead of <img>.

Good explanation. Note howerver, the use of helpers are optional. One
can certainly do without the bells and whistles, especially if you are at all
concerned about page rendering speed. While I do use some helpers I try
to use them as few times as possible, but that is just my preference.

-- Long
http://MeandmyCity.com/ - Find your way
http://edgesoft.ca/blog/read/2 - No-Cookie Session Support plugin