Forum: Ruby on Rails image_tag cannot show picture

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Valentino L. (Guest)
on 2008-12-10 14:16
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

The HTML source code generated
<img alt="Green" src="/images/green.JPG.?1228211220" />

I cannot identify the problem. Please help

Thank you very much

Valentino
Priya B. (Guest)
on 2008-12-10 14:40
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.
Phlip (Guest)
on 2008-12-10 14:44
(Received via mailing list)
> The HTML source code generated
> <img alt="Green" src="/images/green.JPG.?1228211220" />

Does

   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...
Valentino L. (Guest)
on 2008-12-10 15:04
Phlip wrote:
>> The HTML source code generated
>> <img alt="Green" src="/images/green.JPG.?1228211220" />
>
> Does
>
>    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
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
Valentino L. (Guest)
on 2008-12-10 15:15
> 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
<img alt="Dashboard" src="/images/dashboard.jpg.?1228909182" />
Priya B. (Guest)
on 2008-12-10 15:15
Change the extension to lowercase and check it
Martijn Vos (Guest)
on 2008-12-10 15:22
(Received via mailing list)
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
> 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.
Valentino L. (Guest)
on 2008-12-11 04:18
> 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?
Valentino L. (Guest)
on 2008-12-11 04:27
>>
>> 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
Valentino L. (Guest)
on 2008-12-11 04:46
> 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
Valentino L. (Guest)
on 2008-12-11 05:40
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
Martijn Vos (Guest)
on 2008-12-11 10:33
(Received via mailing list)
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.
Valentino L. (Guest)
on 2008-12-11 10:45
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
This topic is locked and can not be replied to.