# Image\_tag cannot show picture

**URL:** <https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308>\
**Category:** Rails\
**Created:** [December 10, 2008, 1:16pm UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308 "2008-12-10T13:16:29Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 10, 2008, 1:16pm UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/1 "2008-12-10T13:16:29Z")

</div>

Dear all

I am new to rails.

In view, I place the following code, but cannot show the picture in the  
webpage  
\<%= image\_tag(“green.JPG”) %\>

However, I can access the picture in this url  
[http://localhost:3000/images/green.JPG](http://localhost:3000/images/green.JPG)

The HTML source code generated  
 ![Green](https://www.ruby-forum.com/images/green.JPG.?1228211220)

I cannot identify the problem. Please help

Thank you very much

Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 10, 2008, 1:40pm UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/2 "2008-12-10T13:40:38Z")

</div>

Do you have the image in the image folder or any other folder?

Also check the extension of that image name(Whether  
uppercase(“green.JPG”) or lowercase(“green.jpg”)). Give correctly.

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 10, 2008, 1:44pm UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/3 "2008-12-10T13:44:29Z")

</div>

> The HTML source code generated  
> ![Green](https://www.ruby-forum.com/images/green.JPG.?1228211220)

Does

[http://localhost:3000/images/green.JPG.?1228211220](http://localhost:3000/images/green.JPG.?1228211220)

work?

And what is that dot . doing after the JPG?

Next, always use lower case file extensions - jpg. That’s just for  
style, but it  
helps us diagnose problems…

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 10, 2008, 2:04pm UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/4 "2008-12-10T14:04:22Z")

</div>

Phlip wrote:

> > The HTML source code generated  
> > ![Green](https://www.ruby-forum.com/images/green.JPG.?1228211220)
> 
> Does
> 
> [http://localhost:3000/images/green.JPG.?1228211220](http://localhost:3000/images/green.JPG.?1228211220)
> 
> work?
> 
> And what is that dot . doing after the JPG?
> 
> Next, always use lower case file extensions - jpg. That’s just for  
> style, but it  
> helps us diagnose problems…

[http://localhost:3000/images/green.JPG.?1228211220](http://localhost:3000/images/green.JPG.?1228211220)  
not work, error message as below, I don’t know why there is a “.”  
appended after the filename.  
No route matches “/images/green.JPG.” with {:method=\>:get}

The image is located at public\images  
The filename is green.JPG

Thank you for your help.  
Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 10, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/5 "2008-12-10T14:15:04Z")

</div>

> [http://localhost:3000/images/green.JPG.?1228211220](http://localhost:3000/images/green.JPG.?1228211220)  
> not work, error message as below, I don’t know why there is a “.”  
> appended after the filename.  
> No route matches “/images/green.JPG.” with {:method=\>:get}
> 
> The image is located at public\images  
> The filename is green.JPG
> 
> Thank you for your help.  
> Valentino

I renamed the file extension to lower case. i.e. green.jpg  
Not work either  
 ![Dashboard](https://www.ruby-forum.com/images/dashboard.jpg.?1228909182)

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 10, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/6 "2008-12-10T14:15:42Z")

</div>

Change the extension to lowercase and check it

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 10, 2008, 2:22pm UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/7 "2008-12-10T14:22:11Z")

</div>

On Dec 10, 2008, at 2:04 PM, Valentino L. wrote:

> > And what is that dot . doing after the JPG?
> > 
> > Next, always use lower case file extensions - jpg. That’s just for  
> > style, but it  
> > helps us diagnose problems…
> 
> [http://localhost:3000/images/green.JPG.?1228211220](http://localhost:3000/images/green.JPG.?1228211220)  
> not work, error message as below, I don’t know why there is a “.”  
> appended after the filename.  
> No route matches “/images/green.JPG.” with {:method=\>:get}

Sounds to me like that extra . is the problem. The big question  
is why image\_tag adds that extra dot. It shouldn’t. Have you  
looked at the source code for image\_tag? You didn’t accidentally  
override it or anything like that?

mcv.

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 11, 2008, 3:18am UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/8 "2008-12-11T03:18:25Z")

</div>

> Sounds to me like that extra . is the problem. The big question  
> is why image\_tag adds that extra dot. It shouldn’t. Have you  
> looked at the source code for image\_tag? You didn’t accidentally  
> override it or anything like that?
> 
> mcv.

Thank you for your help  
Could you tell me where the source code located?

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 11, 2008, 3:27am UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/9 "2008-12-11T03:27:29Z")

</div>

> > mcv.
> 
> Thank you for your help  
> Could you tell me where the source code located?

I found the source code

```
  def image_tag(source, options = {})
    options.symbolize_keys!

    options[:src] = path_to_image(source)
    options[:alt] ||= File.basename(options[:src], 

```

‘.\*’).split(‘.’).first.to\_s.capitalize  
…SKIP…

Since I am new to ruby, and I am a green programmer…I cannot figure  
out the problem. Can you help me?

Thanks alot.  
Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 11, 2008, 3:46am UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/10 "2008-12-11T03:46:12Z")

</div>

> Since I am new to ruby, and I am a green programmer…I cannot figure  
> out the problem. Can you help me?
> 
> Thanks alot.  
> Valentino

I tried the following code, there is also a extra “.”  
\<%= image\_tag(“green.jpg”, :alt =\> “Green”) %\>  
I am frustrated. Please help…

Thank you.  
Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 11, 2008, 4:40am UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/11 "2008-12-11T04:40:11Z")

</div>

Hello all

I think I identified the problem now

In asset\_tag\_help.rb, the problem might cause from \*\*\*\*\*(see below  
code). I am using Rails 2.1.2, I will try the latest version to see the  
problem has been fixed. Thanks all

```
  def image_path(source)
    compute_public_path(source, 'images')
  end

    def compute_public_path(source, dir, ext = nil, include_host = 

```

true)  
has\_request = @controller.respond\_to?(:request)

```
      cache_key =
        if has_request
          [ @controller.request.protocol,
            ActionController::Base.asset_host.to_s,
            @controller.request.relative_url_root,
            dir, source, ext, include_host ].join
        else
          [ ActionController::Base.asset_host.to_s,
            dir, source, ext, include_host ].join
        end

      ActionView::Base.computed_public_paths[cache_key] ||=
        begin
      ***** source += ".#{ext}" if ext && 

```

File.extname(source).blank? || File.exist?(File.join(ASSETS\_DIR, dir,  
“#{source}.#{ext}”))

```
          if source =~ %r{^[-a-z]+://}
            source
          else
            source = "/#{dir}/#{source}" unless source[0] == ?/
            if has_request
              unless source =~ 

```

%r{^#{@controller.request.relative\_url\_root}/}  
source =  
“#{@controller.request.relative\_url\_root}#{source}”  
end  
end

```
            rewrite_asset_path(source)
          end
        end
```

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 11, 2008, 9:45am UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/12 "2008-12-11T09:45:45Z")

</div>

Martijn Vos wrote:

> On Dec 11, 2008, at 4:40 AM, Valentino L. wrote:
> 
> > I think I identified the problem now
> 
> Have you also been able to fix it?
> 
> > ```
> > def compute_public_path(source, dir, ext = nil, include_host =
> > [ ActionController::Base.asset_host.to_s,
> > dir, source, ext, include_host ].join
> > end
> > 
> > ActionView::Base.computed_public_paths[cache_key] ||=
> > begin
> > ***** source += ".#{ext}" if ext &&
> > 
> > ```
> > 
> > File.extname(source).blank? || File.exist?(File.join(ASSETS\_DIR, dir,  
> > “#{source}.#{ext}”))
> 
> The thing is, source += “.#{ext}” will only be executed if ext is  
> not nil. In this case, I think ext is nil, so this should be causing  
> the problem.
> 
> But if something is really mysterious (and in Rails it often is),  
> then I just add lots of debug statements and see what really happens.  
> puts all the names of methods that are called, and all relevant  
> variables (or just use the debugger, of course!), and see where the  
> dot is added. You may have found a bug in Rails.
> 
> > ```
> > end
> > 
> > rewrite_asset_path(source)
> > end
> > end
> > 
> > ```
> 
> All this code is also capable of changing source. I’ve got no idea  
> what it does, but if you just puts the value of source after each  
> one, you’ll see where the dot gets added.
> 
> That’s the only advice I can give you. Other than that, I’m  
> stumped.
> 
> mcv.

Thank you mcv

I solved the problem finally by update the rails to 2.2.2  
I also learned a lesson when I was trying to identify the problem.

Thank you so much  
Valentino

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [December 11, 2008, 9:33am UTC](https://www.ruby-forum.com/t/image-tag-cannot-show-picture/155308/13 "2008-12-11T09:33:14Z")

</div>

On Dec 11, 2008, at 4:40 AM, Valentino L. wrote:

> I think I identified the problem now

Have you also been able to fix it?

> ```
> def compute_public_path(source, dir, ext = nil, include_host =
> [ ActionController::Base.asset_host.to_s,
> dir, source, ext, include_host ].join
> end
> 
> ActionView::Base.computed_public_paths[cache_key] ||=
> begin
> ***** source += ".#{ext}" if ext &&
> 
> ```
> 
> File.extname(source).blank? || File.exist?(File.join(ASSETS\_DIR, dir,  
> “#{source}.#{ext}”))

The thing is, source += “.#{ext}” will only be executed if ext is  
not nil. In this case, I think ext is nil, so this should be causing  
the problem.

But if something is really mysterious (and in Rails it often is),  
then I just add lots of debug statements and see what really happens.  
puts all the names of methods that are called, and all relevant  
variables (or just use the debugger, of course!), and see where the  
dot is added. You may have found a bug in Rails.

> ```
> end
> 
> rewrite_asset_path(source)
> end
> end
> 
> ```

All this code is also capable of changing source. I’ve got no idea  
what it does, but if you just puts the value of source after each  
one, you’ll see where the dot gets added.

That’s the only advice I can give you. Other than that, I’m  
stumped.

mcv.
