Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Cannot display images


  • Please log in to reply
1 reply to this topic

#1 dailammoc

dailammoc

    Dispatcher

  • Members
  • 36 posts
  • Location/

Posted 12 March 2014 - 02:10 AM

I'm reading Agile Web Development with Rails and I had problem with showing images to browser. I followed correctly to the instruction, but my web did not work well. Here is my seeds.rb file:

#---
# Excerpted from "Agile Web Development with Rails",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material, 
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose. 
# Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
#---
#---
# Excerpted from "Agile Web Development with Rails, 4rd Ed.",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material, 
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose. 
# Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
#---
# encoding: utf-8
Product.delete_all
Product.create(:title => 'Web Design for Developers',
    :description =>
      %{<p>
        <em>Web Design for Developers</em> will show you how to make your
        web-based application look professionally designed. We'll help you
        learn how to pick the right colors and fonts, avoid costly interface
        and accessibility mistakes -- your application will really come alive.
        We'll also walk you through some common Photoshop and CSS techniques
        and work through a web site redesign, taking a new design from concept
        all the way to implementation.
      </p>},
    :image_url =>   '/images/wd4d.jpg',
    :price => 42.95)
# . . .
Product.create(:title => 'Programming Ruby 1.9',
    :description =>
      %{<p>
        Ruby is the fastest growing and most exciting dynamic language
        out there. If you need to get working programs delivered fast,
        you should add Ruby to your toolbox.
      </p>},
    :image_url => '/assets/images/ruby.jpg',
    :price => 49.50)
# . . .

Product.create(:title => 'Rails Test Prescriptions',
    :description =>
      %{<p>
        <em>Rails Test Prescriptions</em> is a comprehensive guide to testing
        Rails applications, covering Test-Driven Development from both a
        theoretical perspective (why to test) and from a practical perspective
        (how to test effectively). It covers the core Rails testing tools and
        procedures for Rails 2 and Rails 3, and introduces popular add-ons,
        including Cucumber, Shoulda, Machinist, Mocha, and Rcov.
      </p>},
    :image_url => 'images/rtp.jpg',
    :price => 43.75)

and here is my index.html.erb file:

<div id="product_list" >
  <h1>Listing products</h1>
  <table>
    <% @products.each do |product| %>
        <tr class=" <%= cycle('list_line_odd' , 'list_line_even' ) %>" >
          <td>
            <%= image_tag(product.image_url, :class => 'list_image' ) %>
          </td>
          <td class="list_description" >
            <dl>
              <dt><%= product.title %></dt>
              <dd><%= truncate(strip_tags(product.description),
                               :length => 80) %></dd>
            </dl>
          </td>
          <td class="list_actions" >
            <%= link_to 'Show' , product %><br/>
            <%= link_to 'Edit' , edit_product_path(product) %><br/>
            <%= link_to 'Destroy' , product,
                        :confirm => 'Are you sure?' ,
                        :method => :delete %>
      </td>
    </tr>
  <% end %>
</table>
</div>
<br />
<%= link_to 'New product' , new_product_path %>

can someone give me some idea about this?



#2 Alain ANDRE

Alain ANDRE

    Passenger

  • Members
  • 6 posts
  • LocationBordeaux

Posted 07 April 2014 - 09:56 AM

Hi, you are saving images with your asset folder. image_tag goes in asset folder to find your images.

 

image_tag helper works like this :

image_tag("icon.png")
# => <img alt="Icon" src="/assets/icon.png" />

If it still does not work, you can have a look on asset_url helper.

asset_url("img.png", image)

  • dailammoc likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users