我是Rails的新手,为这个简单的问题感到抱歉。我正在尝试从Rails教程(博客实现)在Rails(版本4.2.6)中构建博客应用程序,但是在Postgresql中实现表并将其填充数据后,我无法从Web浏览器中的表中接收数据。
db / schema.rb的代码
ActiveRecord::Schema.define(version: 20160602113240) do
enable_extension "plpgsql"
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "posts", force: :cascade do |t|
t.string "title"
t.text "body"
t.integer "category_id"
t.integer "author_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
文件posts_controller.rb的代码
class PostsController < ApplicationController
def index
@posts = Post.all
end
end
文件index.html.erb的代码:
<h1>Listing posts</h1>
<% @posts.each do |post| %>
<td><%= post.title %></td>
<td><%= post.body %></td>
<% end %>
在网络浏览器(由WEBrick提供的Chrome浏览器)中,我仅看到标头列出帖子
我不知道出了什么问题-完全没有错误。表中的记录存在(由pgAdmin检查),Postgress正常工作。有人可以帮助我吗?
更新:
添加后 <% raise @posts.inspect %>
我在浏览器中收到错误消息
您错过了表格标签。就像是
<h1>Listing posts</h1>
<% if @posts.any? %>
<table>
<% @posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.body %></td>
</tr>
<% end %>
</table>
<% else %>
<p>No posts</p>
<% end %>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句