Image_tag cannot show picture


#1

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
Green

I cannot identify the problem. Please help

Thank you very much

Valentino


#2

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.


#3

The HTML source code generated
Green

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…


#4

Phlip wrote:

The HTML source code generated
Green

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


#5

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


#6

Change the extension to lowercase and check it


#7

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.


#8

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?


#9

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


#10

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


#11

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

#12

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


#13

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.