Rails 4.0项目之——blog
来源:互联网 发布:药智数据cfda39健康网 编辑:程序博客网 时间:2024/06/06 11:47
本文主要根据网络文章来使用rails 4.0来创建blog工程,详情请参考下面的文章,
参考文章:http://guides.ruby-china.org/getting_started.html
以下为完成后的代码
routes.rb:
resources :posts do
resources :comments
end
db/schema
ActiveRecord::Schema.define(version: 20130802012529) do
create_table "comments", force: true do |t|
t.string "commenter"
t.text "body"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "comments", ["post_id"], name: "index_comments_on_post_id"
create_table "posts", force: true do |t|
t.string "name"
t.string "title"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
end
end
posts_controller.rb
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
http_basic_authenticate_with :name => "qa", :password => "test", :except => [:index, :show]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render action: 'show', status: :created, location: @post }
else
format.html { render action: 'new' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:name, :title, :content)
end
end
comments_controller.rb
class CommentsController < ApplicationController
#before_action :set_comment, only: [:destroy]
http_basic_authenticate_with :name => "qa", :password => "test", :only => :destroy
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(comment_params)
redirect_to post_path(@post)
end
def destroy
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.destroy
redirect_to post_path(@post)
end
private
# Use callbacks to share common setup or constraints between actions.
def set_comment
@comment = Comment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def comment_params
params.require(:comment).permit(:commenter, :body, :post_id)
end
end
其他的代码可以参考上面的文章
- Rails 4.0项目之——blog
- Blog项目 —上集
- Blog项目—下集
- Rails 入门 —— 新建一个blog程序
- 天使之家(angel_nest)安装指南——rails开源项目赏
- Rails实现一个blog项目01-基本功能blog和comment的实现
- Rails实现一个blog项目01-基本功能blog和comment的实现
- Rails CMS 个人blog
- shopqi——rails开源项目赏
- Ruby on Rails 之旅(七)—— Ruby on Rails 入门(6)
- Ruby on Rails 之旅(七)—— Ruby on Rails 入门(5)
- Ruby on Rails 之旅(七)—— Ruby on Rails 入门(4)
- Ruby on Rails 之旅(七)—— Ruby on Rails 入门(2)
- Rails实现一个blog项目02-登录功能和session的实现
- Rails实现一个blog项目02-登录功能和session的实现
- poj1363——Rails
- poj1363——Rails
- Uva514——Rails
- Shell脚本编程的常识
- pl/sql developer配置
- 批量设置会计月脚本,主要用于示例ORACLE时间函数
- 第四章函数--函数初体验项目1求四数的最大公约数
- 包含min函数的栈
- Rails 4.0项目之——blog
- linux进程调度 - cpu进程负载均衡
- 函数指针(转)
- 关于 取得 答案 answer 的 English
- DownloadManager使用
- windows 7 安装 mysql 5.6 启动服务
- 栈的链表实现
- Linux 命令 大结
- android内存泄露的问题