[ruby on rails] 跟我学之(5)显示所有数据

之前的index页,显示的是hello world,现在将其修改为显示我们在rails console里面录入的数据。

1. 修改action

如之前的章节《[ruby on rails] 跟我学之路由映射》里面介绍的,修改app/controllers/posts_controller.rb 的index方法,如下:

  def index
    @posts =  Post.all
  end

 

2. 修改view

并修改app/views/posts/index.html.erb 这个view文件,如下:

<h1>Our blogs</h1>
<% @posts.each do |post| %>
  <h2><%=post.title%></h2>
  <%=post.context%><br/><hr/>
<% end %>

 

3. 运行服务

执行指令 rails s, 打开 http://localhost:3000/posts即可,如下图:

 

转载请注明本文来自:http://www.cnblogs.com/Tommy-Yu/p/4141207.html 谢谢!

posted @ 2014-12-03 20:47  tommy.yu  阅读(536)  评论(0编辑  收藏  举报