Images working / showing in the new Rails application

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)
+4
source share
4 answers

So, I worked on this book, and you need to make changes in 2 places.

In the seed file, just specify the name jpg. Since you use rails 3.1, everything inside assets / images / can only be connected by name.

You will also have to make changes to the views. Download the source for the book here http://pragprog.com/titles/rails4/source_code and see what they have for depot 2 to represent the product index #. They are really messy with this release.

+6
source

Check out the Rails 3.1 Asset Pipeline Guide . In section 2.2, Asset Link Encoding, you will find the following:

In regular views, you can access the images in the resource / image directory as follows:

<%= image_tag "rails.png" %>

As I understand your code (maybe I'm wrong here), you are trying to save the file name in the database and show this image later along with other object data. Therefore, you should try to remove the path from it, use only the file name. And then we denote it using the image_tag method.

+3
source

I had the same problem, and I realized that when I downloaded the test images for the Depot application, they were saved as XXX.jpeg instead of XXX.jpg. As a result, the URL in seeds.rb did not match the file names in the assets / images.

+1
source

The only thing that worked for me was:

 <%= image_tag('/assets/images/' + product.image_url, class: 'list_image') %> 
+1
source

All Articles