Ruby on Rail 实现 REST And ActiveResource

rails new blog //创建应用

cd blog //进入应用目录

rails generate scaffold Post name:stringtitle:stringcontent:text

rake db:migrate //创建数据表

rails server -p 888888

4. cd ..

5 rails new notebook

6 cd notebook

7 rails generate scaffold book name:string

rake db:migrate //创建数据表

在Mode目录 下创建一个 Post.rb

class Post < ActiveResource::Base

self.site = 'http://localhost:888888'

end

这样在controller里,可以调用post的方法了,比如Post.new或者Post.all

创建 welcomeController

$ rails generate controller welcome index

有些问题查看前一篇文章

打开 welcomecontroller.rb编辑

class WelcomeController < ApplicationController

def index

# notes = Note.find :all

notes = Note.all

puts "I see #{notes.size} note(s):"

notes.each do |note|

puts " #{note.date}: #{note.body}"

end

end

end

读者可以任意修改

rails server

http://localhost:3000/

终端下出现了

[2013-01-23 21:23:10] INFO WEBrick::HTTPServer#start: pid=4683 port=3000

I see 2 note(s):

2013-01-23: 你好,世界

2013-01-23: 很不错哦

证明成功了