Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Images working/showing in my new Rails app

Update: o.k. this is strange, in the brower, i can access the images at localhost:3000/assets/images/rails.png but when I put that path in the seeds.rb file and then load the page, it shows that it's trying to find the image in /assets/rails.png i.e. it's skipping over the images folder...any ideas?

Is it possible that rails is configured somewhere to only look into two folders?

I'm using a book called Agile web Development with Rails to learn how to use the framework but there seem to be some slight differences. It supplies code for rails 3.0 and 3.1 (I'm using the latter) but it's not always working as expected. Thus far we have created scaffolding for Products class and used seeds.rb to put some data in the sqlite3 database. There are images in the assets/images folder for each "product" but it's not showing. I experimented in the seeds.rb file with the path for the images but it didn't work.

Images are in the folder app/assets/images

I will show you the following files, all of which in some way deal with the images

1.app/views/products/_form.html.erb 2.app/views/products/index.html.erb 3. db/seeds.rb

UPDATE: in the logfile, it says 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"):

app/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 %>
  1. 3. db/seeds.rb (notice here that I experimented with 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 Prescriptions', :description => %{

    blah blah

    }, :image_url => '/images/rtp.jpg', :price => 43.75)
like image 463
Leahcim Avatar asked Dec 22 '22 08:12

Leahcim


2 Answers

Ok, So I've been working through this book to and you have to make changes in 2 places.

In the seed file, just reference the name of the jpg. Since you're using rails 3.1, anything inside of assets/images/ can be referenced just by it's name.

You'll have to make a change in the views as well. Download the source for the book here http://pragprog.com/titles/rails4/source_code and look at what they have for depot 2 in for the products#index view. They got really sloppy with this release.

like image 51
StevenNunez Avatar answered Jan 02 '23 20:01

StevenNunez


Have a look at the Rails 3.1 Guide to Asset Pipeline. In section 2.2 Coding Links to Assets, you will find the following:

In regular views you can access images in the assets/images directory like this:

<%= image_tag "rails.png" %>

As I understand your code (I may be wrong here), you try to store the filename of an image in the database, and show that image later together with the other object data. So you should try to remove the path from it, use just the filename. And then denote it by using the method image_tag.

like image 22
mliebelt Avatar answered Jan 02 '23 21:01

mliebelt