使用Ruby On Rails15分钟打造一个博客系统
来源:互联网 发布:网络平台招商加盟 编辑:程序博客网 时间:2024/06/04 18:00
控制台创建一个博客项目
测试: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>
修改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>
增加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
这个时候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
增加删除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
给你的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
- 使用Ruby On Rails15分钟打造一个博客系统
- 使用Ruby On Rails15分钟打造一个博客系统
- ruby on rails10分钟创建一个博客系统!新手必看!
- Ruby on Rails 学习(三)——15分钟搭建一个博客系统
- Ruby On Rails (ROR) 2.0入门编程视频教程-(4)几分钟制作一个简单的BLog博客系统(增删查改操作)
- 10分钟内创建一个 Ruby 博客程序
- 使用 ruby on rails 15分钟创建 Blog
- Ruby on Rails(二) - 初探Ruby on Rails, 搭建一个博客项目并发布(草稿)
- ruby on rails使用
- 5分钟编写一个博客(使用ASP技术)
- linux虚拟机安装ruby和ruby on rails实例(windows系统使用)
- 这是一个关于ruby on rails的博客测试 test
- 十分钟教你打造一个微信语音点歌系统
- 10分钟教你打造一个微信语音点歌系统
- 分分钟搭一个博客
- Ruby On Rails中REST API使用示例——基于云平台+云服务打造自己的在线翻译工具
- SPCAMLEditor使用系列(1)--利用SPCAMLEditor,10分钟打造一个在线视频播放库
- ruby on rails 使用TinyMCE
- strcpy函数的实现
- 硬件-CPU3
- 理解OAuth 2.0
- Java String中若干问题
- 集合中的其他类
- 使用Ruby On Rails15分钟打造一个博客系统
- struct的内存对齐方式
- Android中使用Makefile编译程序和库的方法
- activemq
- linux驱动ioctl
- 2013杭州站C - Zhuge Liang's Password(模拟)
- Basic Calculator II
- unix学习笔记------套接字-----王保明老师的笔记
- iOS巅峰之键盘回收方法