Update: ok this is strange, in brower I can access the images on localhost: 3000 / assets / images / rails.png, but when I put this path in the seeds.rb file and then load the page, it shows that it is trying find the image in the file /assets/rails.png, i.e. it skips the image folder ... any ideas?
Is it possible that the rails are set up somewhere in only two folders?
I am using a book called Agile web Development with Rails to learn how to use the framework, but there seem to be some minor differences. It supplies the code for rails 3.0 and 3.1 (I use the latter), but it does not always work as expected. So far, we have created scaffolding for the Products class and used seeds.rb to host some data in the sqlite3 database. In the "Resources / Images" folder there are images in each "product", but they are not displayed. I experimented in the seeds.rb file with a path for images, but it did not work.
Images are in the app / assets / images folder
I will show you the following files, all of which process images in some way
1.app/views/products/_form.html.erb 2.app/views/products/index.html.erb 3. db / seeds.rb
UPDATE: the log file says that there is a routing error for the images.
Started GET "/assets/wd4d.jpg" for 127.0.0.1 at Tue Sep 27 11:01:43 -0400 2011 Served asset /wd4d.jpg - 404 Not Found (3ms) ActionController::RoutingError (No route matches [GET] "/assets/wd4d.jpg"): Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.1.0/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.5ms) Started GET "/images/ruby.jpg" for 127.0.0.1 at Tue Sep 27 11:01:43 -0400 2011 ActionController::RoutingError (No route matches [GET] "/images/ruby.jpg"):
application / views / products / _form.html.erb
<%= form_for(@product) do |f| %> <% if @product.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2> <ul> <% @product.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %> </div> <div class="field"> <%= f.label :description %><br /> <%= f.text_area :description, :rows => 6 %> </div> <div class="field"> <%= f.label :image_url %><br /> <%= f.text_field :image_url %> </div> <div class="field"> <%= f.label :price %><br /> <%= f.text_field :price %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>
2.app/views/products/index.html.erb
<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), :truncate => 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> <br /> <%= link_to 'New product', new_product_path %>
3. db / seeds.rb (note that I experimented with the image URL)
Product.delete_all Product.create (: title => "Web Design for Developers" ,: description =>% {
blah blah
} ,: image_url => 'wd4d.jpg',
: price => 42.95).,.
Product.create (: title => 'Programming Ruby 1.9' ,: description =>% {
blah blah.
} ,: image_url => '/images/ruby.jpg' ,: price => 49.50).,.
Product.create (: title => 'Rails Test Presets' ,: description =>% {
blah blah
} ,: image_url => '/images/rtp.jpg' ,: price => 43.75)