Search code examples
rubyruby-on-rails-3sqlitewebrick

Rails Server requiring restart to show changes


I'm currently learning Ruby on Rails with the help of the Book "Agile Web Development with Rails", but I have a problem with the first application.

I created the app with rails new depot and generated the scaffold with

rails generate scaffold Product \title:string description:text image_url:string price:decimal

Then I ran rake db:migrate

According to the book I should now be able to create new Products which would then display immediately. But when adding a new Product it does not show up when viewing the url http://localhost:3000/products or http://localhost:3000/products.json, When I view the database file the added products are there.

The added Products show up in the list after I restarted the server. Does anybody know what I've done wrong?

Using: Rails 3.2.4, Ruby 1.9.3, WEBrick 1.3.1, SQLite 3 and Mac OSX

EDIT: Code generated by the scaffold:

   products_controller.rb
      # GET /products
      # GET /products.json
      def index
        @products = Product.all

        respond_to do |format|
          format.html # index.html.erb
          format.json { render json: @products }
        end
      end

index.html.erb  
<h1>Listing products</h1>
<table>
  <tr>
    <th>Title</th>
    <th>Description</th>
    <th>Image url</th>
    <th>Price</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @products.each do |product| %>
  <tr>
    <td><%= product.title %></td>
    <td><%= product.description %></td>
    <td><%= product.image_url %></td>
    <td><%= product.price %></td>
    <td><%= link_to 'Show', product %></td>
    <td><%= link_to 'Edit', edit_product_path(product) %></td>
    <td><%= link_to 'Destroy', product, confirm: 'Are you sure?', method: :delete %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Product', new_product_path %>

Solution

  • I'll bet your Rails version is 3.2.4? Just update your Rails version to 3.2.5+ and you'll be fine.

    It's a regression bug in Rails that happened in 3.2.4. It's been fixed: https://github.com/rails/rails/commit/7056079761eba049bbc6108946a1626fe169dbdf