使用Ruby On Rails15分钟打造一个博客系统

来源:互联网 发布:网络平台招商加盟 编辑:程序博客网 时间:2024/06/04 18:00


控制台创建一个博客项目

rails new blog

cd blog

rails g scaffold post title:string text:text

rails g model comment post_id:integer text:text

rake db:migrate

rails s


测试:http://localhost:3000/posts

       


修改index.html.erb

blog/app/views/posts/index.html.erb

删除原有的内容,新的内容如下:

  1. <h1>Welcome to PTIAN's Blog</h1>  
  2.   
  3. <% @posts.each do |post| %>  
  4.    <h2><%= link_to post.title, post%></h2>  
  5.    <p>  
  6.        <i>-<%= time_ago_in_words post.created_at %> ago </i>  
  7.    </p>      
  8.    <p>  
  9.      <%= truncate post.text %>  
  10.    </p>  
  11. <% end %>  
  12.   
  13. <p>  
  14.    <%= link_to "New Post", new_post_path%>  
  15. </p>      

修改show.html.erb

blog/app/views/posts/show.html.erb

  1. <h1><%= @post.title %></h1>  
  2.   
  3. <%= @post.text %>  
  4.   
  5. <p>  
  6. <%= link_to "Back", posts_path %>  
  7. |  
  8. <%= link_to "Edit", edit_post_path(@post) %>  
  9. |  
  10. <%= link_to "Delete",@post,:method => :delete:confirm => "Are you sure?" %>  
  11. </p>  


增加Comments项

继续修改show.html.erb

blog/app/views/posts/show.html.erb

  1. <h1><%= @post.title %></h1>  
  2.   
  3. <%= @post.text %>  
  4.   
  5. <h2>Comments</h2>  
  6.   
  7. <% @post.comments.each do |comment| %>  
  8.   <p><%= comment.text %></p>  
  9.   <p><%= time_ago_in_words comment.created_at %> ago </p>  
  10. <% end %>  
  11.   
  12. <%= form_for [@post,@post.comments.build] do |f| %>  
  13.   <p><%= f.text_area :text:size => '40x10' %> </p>  
  14.   <p><%= f.submit "Post Comment" %> </p>  
  15. <% end %>  
  16.   
  17. <p>  
  18. <%= link_to "Back", posts_path %>  
  19. |  
  20. <%= link_to "Edit", edit_post_path(@post) %>  
  21. |  
  22. <%= link_to "Delete",@post,:method => :delete:confirm => "Are you sure?" %>  
  23. </p>  

修改blog/app/models/post.rb

  1. class Post < ActiveRecord::Base  
  2.   attr_accessible :text:title  
  3.   has_many :comments  
  4. end  
修改blog/app/models/comment.rb

  1. class Comment < ActiveRecord::Base  
  2.   attr_accessible :post_id:text  
  3.   belongs_to :post  
  4. end  
修改blog/config/routes.rb

  1. resources :posts do  
  2.     resources :comments  
  3.   end  

这个时候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

  1. class CommentsController < ApplicationController  
  2.   def create  
  3.     @post = Post.find(params[:post_id])  
  4.     @comment = @post.comments.build(params[:comment])  
  5.     @comment.save  
  6.   
  7.     redirect_to @post  
  8.   end  
  9.   
  10.   def destroy  
  11.   end  
  12. end  


增加删除comment功能

修改blog/app/views/posts/show.html.erb

(增加Delete Comment链接)

  1. <h1><%= @post.title %></h1>  
  2.   
  3. <%= @post.text %>  
  4.   
  5. <h2>Comments</h2>  
  6.   
  7. <% @post.comments.each do |comment| %>  
  8.   <p><%= comment.text %></p>  
  9.   <p><%= time_ago_in_words comment.created_at %> ago </p>  
  10.   <p><%= link_to "Delete Comment", [@post, comment], :method => :delete:confirm => "Are you sure?" %></p>  
  11. <% end %>  
  12.   
  13. <%= form_for [@post,@post.comments.build] do |f| %>  
  14.   <p><%= f.text_area :text:size => '40x10' %> </p>  
  15.   <p><%= f.submit "Post Comment" %> </p>  
  16. <% end %>  
  17.   
  18. <p>  
  19. <%= link_to "Back", posts_path %>  
  20. |  
  21. <%= link_to "Edit", edit_post_path(@post) %>  
  22. |  
  23. <%= link_to "Delete",@post,:method => :delete:confirm => "Are you sure?" %>  
  24. </p>  
修改blog\app\controllers\comments_controller.rb

  1. class CommentsController < ApplicationController  
  2.   def create  
  3.     @post = Post.find(params[:post_id])  
  4.     @comment = @post.comments.build(params[:comment])  
  5.     @comment.save  
  6.   
  7.     redirect_to @post  
  8.   end  
  9.   
  10.   def destroy  
  11.     @comment = Comment.find(params[:id])  
  12.     @comment.destroy  
  13.   
  14.     redirect_to @comment.post  
  15.   end  
  16. end  


给你的blog添加ATOM feed

修改blog\app\controllers\posts_controller.rb

  def index
    @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


0 0
原创粉丝点击