【问题标题】:Rails Server requiring restart to show changesRails Server 需要重新启动才能显示更改
【发布时间】:2012-06-14 15:11:52
【问题描述】:

我目前正在借助《Agile Web Development with Rails》一书学习 Ruby on Rails,但我在第一个应用程序上遇到了问题。

我用 rails new depot 并使用

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

然后我跑了rake db:migrate

根据这本书,我现在应该能够创建新产品,然后立即显示。 但是当添加一个新产品时,它在查看 url 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 %>

【问题讨论】:

  • 您是如何将产品添加到表中的?
  • 我用自动创建的标准新产品链接添加了它。
  • 您能告诉我们您是如何渲染/获取我们的产品的吗?我的意思是你的动作控制器。您的服务器是否以开发模式启动?
  • 我添加了代码,使用 -e development 以开发模式启动服务器并没有改变任何事情。另外我不认为这是我所处环境的问题:当我重新加载页面时,功能索引应该再次加载产品,但终端服务器输出显示它没有再次加载它们,只有在我重新启动后一次服务器。刷新产品页面后显示新插入的行也应该在生产环境中工作。
  • 没人?为什么控制器不查询数据库?它总是显示 ActiveRecord: 0.0ms

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


【解决方案1】:

我敢打赌你的 Rails 版本是 3.2.4?只需将 Rails 版本更新到 3.2.5+ 就可以了。

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

【讨论】:

    猜你喜欢
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    相关资源
    最近更新 更多