Rails服务器需要重新启动才能显示更改

时间:2012-06-05 22:19:05

标签: ruby ruby-on-rails-3 sqlite webrick

我目前正在使用“使用Rails进行Agile Web开发”一书的帮助学习Ruby on Rails,但我对第一个应用程序有疑问。

我创建了应用程序 rails new depot 并用

生成了脚手架
rails generate scaffold Product \title:string description:text image_url:string price:decimal

然后我跑了rake db:migrate

根据这本书,我现在应该可以创建新的产品,然后立即显示。 但是在添加新产品时,在查看网址http://localhost:3000/productshttp://localhost:3000/products.json时,它不会显示,当我查看数据库文件时,添加的产品就在那里。

重新启动服务器后,添加的产品会显示在列表中。 有人知道我做错了吗?

使用:Rails 3.2.4,Ruby 1.9.3,WEBrick 1.3.1,SQLite 3和Mac OSX

编辑:脚手架生成的代码:

   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 %>

1 个答案:

答案 0 :(得分:1)

我敢打赌你的Rails版本是3.2.4?只需将您的Rails版本更新为3.2.5+即可。

这是Rails中的一个回归错误,发生在3.2.4中。它已被修复:https://github.com/rails/rails/commit/7056079761eba049bbc6108946a1626fe169dbdf