控制台创建一个博客项目
测试:http://localhost:3000/posts
修改index.html.erb
blog/app/views/posts/index.html.erb
删除原有的内容,新的内容如下:
- <h1>Welcome to PTIAN's Blog</h1>
- <% @posts.each do |post| %>
- <h2><%= link_to post.title, post%></h2>
- <p>
- <i>-<%= time_ago_in_words post.created_at %> ago </i>
- </p>
- <p>
- <%= truncate post.text %>
- </p>
- <% end %>
- <p>
- <%= link_to "New Post", new_post_path%>
- </p>
![](https://img-my.csdn.net/uploads/201304/06/1365220778_4018.jpg)
修改show.html.erb
blog/app/views/posts/show.html.erb
- <h1><%= @post.title %></h1>
- <%= @post.text %>
- <p>
- <%= link_to "Back", posts_path %>
- |
- <%= link_to "Edit", edit_post_path(@post) %>
- |
- <%= link_to "Delete",@post,:method => :delete, :confirm => "Are you sure?" %>
- </p>
![](https://img-my.csdn.net/uploads/201304/06/1365221754_7899.jpg)
增加Comments项
继续修改show.html.erb
blog/app/views/posts/show.html.erb
- <h1><%= @post.title %></h1>
- <%= @post.text %>
- <h2>Comments</h2>
- <% @post.comments.each do |comment| %>
- <p><%= comment.text %></p>
- <p><%= time_ago_in_words comment.created_at %> ago </p>
- <% end %>
- <%= form_for [@post,@post.comments.build] do |f| %>
- <p><%= f.text_area :text, :size => '40x10' %> </p>
- <p><%= f.submit "Post Comment" %> </p>
- <% end %>
- <p>
- <%= link_to "Back", posts_path %>
- |
- <%= link_to "Edit", edit_post_path(@post) %>
- |
- <%= link_to "Delete",@post,:method => :delete, :confirm => "Are you sure?" %>
- </p>
修改blog/app/models/post.rb
- class Post < ActiveRecord::Base
- attr_accessible :text, :title
- has_many :comments
- end
- class Comment < ActiveRecord::Base
- attr_accessible :post_id, :text
- belongs_to :post
- end
- resources :posts do
- resources :comments
- end
![](https://img-my.csdn.net/uploads/201304/06/1365223016_5614.jpg)
这个时候Comment就出来了,但是如果提交comment,还会报错,因为我们还没写comment的controller
创建comment的controller
打开一个新的命令行
D:\Ruby\projects>cd blog
D:\Ruby\projects\blog>rails g controller comments create destroy
打开新创建的blog\app\controllers\comments_controller.rb
- class CommentsController < ApplicationController
- def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.build(params[:comment])
- @comment.save
- redirect_to @post
- end
- def destroy
- end
- end
![](https://img-my.csdn.net/uploads/201304/06/1365224240_9466.jpg)
增加删除comment功能
修改blog/app/views/posts/show.html.erb
(增加Delete Comment链接)
- <h1><%= @post.title %></h1>
- <%= @post.text %>
- <h2>Comments</h2>
- <% @post.comments.each do |comment| %>
- <p><%= comment.text %></p>
- <p><%= time_ago_in_words comment.created_at %> ago </p>
- <p><%= link_to "Delete Comment", [@post, comment], :method => :delete, :confirm => "Are you sure?" %></p>
- <% end %>
- <%= form_for [@post,@post.comments.build] do |f| %>
- <p><%= f.text_area :text, :size => '40x10' %> </p>
- <p><%= f.submit "Post Comment" %> </p>
- <% end %>
- <p>
- <%= link_to "Back", posts_path %>
- |
- <%= link_to "Edit", edit_post_path(@post) %>
- |
- <%= link_to "Delete",@post,:method => :delete, :confirm => "Are you sure?" %>
- </p>
- class CommentsController < ApplicationController
- def create
- @post = Post.find(params[:post_id])
- @comment = @post.comments.build(params[:comment])
- @comment.save
- redirect_to @post
- end
- def destroy
- @comment = Comment.find(params[:id])
- @comment.destroy
- redirect_to @comment.post
- end
- end
![](https://img-my.csdn.net/uploads/201304/06/1365224835_7915.jpg)
给你的blog添加ATOM feed
修改blog\app\controllers\posts_controller.rb
@posts = Post.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
format.atom
end
end
修改blog\app\views\layouts\application.html.erb
header区域中添加下面的代码,用于显示Atom的图标
<%= auto_discovery_link_tag :atom, "/posts.atom" %>
一个博客就基本完成了。
Source Code:15mins_blog_sourcecode_ptian.zip
转载出处:http://blog.csdn.net/pan_tian/article/details/8763627