ruby on rails 创建一个博客项目_如何创建一个博客
ruby on rails 创建一个博客项目由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“如何创建一个博客”。
控制台创建一个博客项目
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.2.3.4.5.6.7.8.9.10.> 11.
来源: http://blog.csdn.net/pan_tian/article/details/8763627 <%= @post.title %>
<%= @post.text %>
<%= link_to “Back”, posts_path %> |
<%= link_to “Edit”, edit_post_path(@post)%> |
<%= link_to “Delete”,@post,:method => :delete, :confirm => “Are you sure?” %
增加Comments项 继续修改show.html.erb blog/app/views/posts/show.html.erb 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.> <%= @post.title %>
<%= @post.text %>
Comments
<% @post.comments.each do |comment| %>
<%= comment.text %>
<%= time_ago_in_words comment.created_at %> ago
<% end %><%= form_for [@post,@post.comments.build] do |f| %>
<%= f.text_area :text, :size => '40x10' %>
<%= f.submit “Post Comment” %>
<% end %><%= link_to “Back”, posts_path %> |
<%= link_to “Edit”, edit_post_path(@post)%> |
<%= link_to “Delete”,@post,:method => :delete, :confirm => “Are you sure?” %23.
1.2.修改blog/app/models/post.rb 增加
has_many :comments
修改blog/app/models/comment.rb 增加 belongs_to :post 修改blog/config/routes.rb
1.2.3.resources :posts do
resources :comments
end
这个时候Comment就出来了,但是如果提交comment,还会报错,因为我们还没写comment的controller 打开一个新的命令行 D:Rubyprojects>cd blog
D:Rubyprojectsblog>rails g controller comments create destroy
打开新创建的blogappcontrollerscomments_controller.rb
1.cla CommentsController
5.6.7.8.9.def create
@post = Post.find(params[:post_id])@comment = @post.comments.build(comment_params)@comment.save
redirect_to @post end
def comment_params
params.require(:comment).permit(:id, :text)end
10.11.def destroy
end
12.13.end
14.15.增加删除comment功能
修改blog/app/views/posts/show.html.erb(增加Delete Comment链接)1.2.3.4.5.<%= @post.title %>
<%= @post.text %>
Comments 6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.ure?“ %> 24.<% @post.comments.each do |comment| %>
<%= comment.text %>
<%= time_ago_in_words comment.created_at %> ago
<%= link_to ”Delete Comment“, [@post, comment], :method => :delete, :confirm => ”Are you sure?“ %>
<% end %>
<%= form_for [@post,@post.comments.build] do |f| %>
<%= f.text_area :text, :size => '40x10' %>
<%= f.submit ”Post Comment“ %>
<% end %><%= link_to ”Back“, posts_path %> |
<%= link_to ”Edit“, edit_post_path(@post)%> |
<%= link_to ”Delete“,@post,:method => :delete, :confirm => ”Are you s
修改blogappcontrollerscomments_controller.rb cla CommentsController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(comment_params)
@comment.save
redirect_to @post
end
def destroy
@comment = Comment.find(params[:id])
@comment.destroy
redirect_to @comment.post
end end
def comment_params params.require(:comment).permit(:id, :text)end
给你的blog添加ATOM feed
修改blogappcontrollersposts_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 修改blogappviewslayoutsapplication.html.erb header区域中添加下面的代码,用于显示Atom的图标 <%= auto_discovery_link_tag :atom, “/posts.atom” %>
一个博客就基本完成了。