Rails 2.0.1 Todo List(2.0的scaffold?)

在别处看到的,感觉挺有趣,转一下

rails todo
cd todo
rake db:create:all(在这之前要把数据库的用户名和密码输入正确)
ruby script/generate scaffold Todo title:string body:text done:boolean due:datetime
rake db:migrate

开始服务
ruby script/server

打开浏览器输入并访问http://localhost:3000/todos

这是生成的todos_controller.rb
[code]
class TodosController < ApplicationController
# GET /todos
# GET /todos.xml
def index
@todos = Todo.find(:all)

respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @todos }
end
end

# GET /todos/1
# GET /todos/1.xml
def show
@todo = Todo.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @todo }
end
end

# GET /todos/new
# GET /todos/new.xml
def new
@todo = Todo.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @todo }
end
end

# GET /todos/1/edit
def edit
@todo = Todo.find(params[:id])
end

# POST /todos
# POST /todos.xml
def create
@todo = Todo.new(params[:todo])

respond_to do |format|
if @todo.save
flash[:notice] = 'Todo was successfully created.'
format.html { redirect_to(@todo) }
format.xml { render :xml => @todo, :status => :created, :location => @todo }
else
format.html { render :action => "new" }
format.xml { render :xml => @todo.errors, :status => :unprocessable_entity }
end
end
end

# PUT /todos/1
# PUT /todos/1.xml
def update
@todo = Todo.find(params[:id])

respond_to do |format|
if @todo.update_attributes(params[:todo])
flash[:notice] = 'Todo was successfully updated.'
format.html { redirect_to(@todo) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @todo.errors, :status => :unprocessable_entity }
end
end
end

# DELETE /todos/1
# DELETE /todos/1.xml
def destroy
@todo = Todo.find(params[:id])
@todo.destroy

respond_to do |format|
format.html { redirect_to(todos_url) }
format.xml { head :ok }
end
end
end

[/code]

自动生成的001_create_todos.rb
[code]
class CreateTodos < ActiveRecord::Migration
def self.up
create_table :todos do |t|
t.string :title
t.text :body
t.boolean :done
t.datetime :due

t.timestamps
end
end

def self.down
drop_table :todos
end
end
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值